每日一经
每天收集一个java日常能用到的解决问题的方法,以后方便查阅。
实现
java8环境,有两个类 Temporal和TemporalAdjuster。提供了一些方法:
获取时间:
LocalDate (date without a time zone)
LocalTime (time without a time zone)
LocalDateTime (date-time without a time zone)
获取第一天和最后一天
firstDayOfMonth() (return the first day of the current month)
lastDayOfMonth() (return the last day of the current month)
firstDayOfNextMonth() (return the first day of the next month)
firstDayOfNextYear() (return the first day of the next year)
具体代码实例
1 初始化日期
LocalDate date = LocalDate.of(2021, Month.FEBRUARY, 27);
2 获取对应日期月份第一天
// 2021-02-01
LocalDate firstDayOfFeb = date.with(TemporalAdjusters.firstDayOfMonth());
3 获取对应日期月份最后一天
// 2021-02-28
LocalDate lastDayOfFeb = date.with(TemporalAdjusters.lastDayOfMonth());
网友评论