使用`LocalDateTime`类和`LocalDate`类来实现上述需求。首先,我们可以通过`LocalDateTime.now()`获取当前时间,然后判断当前时间是否早于或等于每月2号的0点,如果是,则将时间修改为当年的1月1号的0点。\n\n下面是用Groovy代码实现的示例:\n\ngroovy\nimport java.time.LocalDateTime\nimport java.time.LocalDate\nimport java.time.Month\n\nLocalDateTime now = LocalDateTime.now()\nLocalDateTime targetDateTime = LocalDateTime.of(now.year, Month.JANUARY, 1, 0, 0, 0, 0)\n\nif (now <= targetDateTime) {\n targetDateTime = LocalDateTime.of(now.year, Month.JANUARY, 1, 0, 0, 0, 0)\n}\n\nprintln(targetDateTime)\n\n\n这段代码中,我们使用`LocalDateTime.now()`获取当前时间,并且使用`LocalDateTime.of()`构造一个目标时间`targetDateTime`,设置为当年的1月1号的0点。然后,我们判断如果当前时间`now`早于或等于`targetDateTime`,则将`targetDateTime`修改为当年的1月1号的0点。\n\n最后,我们输出`targetDateTime`的值。注意,这里输出的时间是按照默认格式进行输出的,如果需要以其他格式输出,可以使用`DateTimeFormatter`类来进行格式化。

Groovy 获取每月2号0点时间并调整时间示例

原文地址: https://www.cveoy.top/t/topic/pVvC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录