Groovy:如何手动创建日期? - GroovyRuntimeException

问题描述

我想使用 Groovy 从 web api 导出一些数据。因此我想手动设置一个我想开始的日期。例如,我想从 2020 年 12 月 31 日开始。

因此我使用了这个代码

import java.time.LocalDateTime;
LocalDateTime fromDate = new LocalDateTime(2020,12,31,10,00);

执行脚本时,我得到一个 GroovyRuntimeException:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.time.LocalDateTime(Integer,Integer,Integer)

解决方法

LocalDateTime 没有公共构造函数。使用众多工厂 of() 方法之一:

LocalDateTime fromDate = LocalDateTime.of(2020,12,31,10,0)
//or
LocalDateTime fromDate = LocalDateTime.of(LocalDate.of(2020,31),LocalTime.of(10,0))
//etc.