Java ChronoZonedDateTime类的with(TemporalAdjuster)方法返回使用指定调整程序调整此日期时间对象的新日期时间对象。这个方法会在调整程序的作用下返回与当前日期时间对象不同的日期时间对象。
以下是使用with(TemporalAdjuster)方法的示例代码:
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
public class ChronoZonedDateTimeExample {
public static void main(String[] args) {
// Create a ZonedDateTime object for "Australia/Sydney" timezone
ZonedDateTime dateTime = ZonedDateTime.of(LocalDate.now(), LocalTime.now(), ZoneId.of("Australia/Sydney"));
// Print the current date and time in "Australia/Sydney" timezone
System.out.println("Current date and time in Australia/Sydney: " + dateTime);
// Use TemporalAdjuster to adjust the ZonedDateTime object to last day of the current month
ZonedDateTime lastDayOfMonth = dateTime.with(TemporalAdjusters.lastDayOfMonth());
// Print the date and time on last day of the current month
System.out.println("Date and time on last day of the current month: " + lastDayOfMonth);
}
}
输出结果如下:
Current date and time in Australia/Sydney: 2022-10-04T14:55:21.067054+11:00[Australia/Sydney]
Date and time on last day of the current month: 2022-10-31T14:55:21.067054+11:00[Australia/Sydney]
在上面的示例中,我们使用with(TemporalAdjusters.lastDayOfMonth())方法将当前日期时间调整为本月的最后一天。这样,我们就得到了一个新的ZonedDateTime对象,其日期和时间为本月的最后一天。
免责声明:本文内容通过AI工具匹配关键字智能整合而成,仅供参考,火山引擎不对内容的真实、准确或完整作任何形式的承诺。如有任何问题或意见,您可以通过联系service@volcengine.com进行反馈,火山引擎收到您的反馈后将及时答复和处理。