个人的LocalDate日期工具类(持续更新
本人在工作中使用到的日期工具类方法,想起来的话就更新一下,有需要的朋友的参考借鉴一下。
日期工具类LocalDateUtil
public class LocalDateUtil{
public static Date toDate(LocalDate localDate) {
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zoneId).toInstant();
return Date.from(instant);
}
public static Date toDate(LocalDateTime localDateTime) {
ZoneId zoneId = ZoneId.systemDefault();
return Date.from(localDateTime.atZone(zoneId).toInstant());
}
public static LocalDate getFirstDayOfMonth(Integer year, Month month) {
return LocalDate.of(year, month, 1);
}
public static LocalDate getFirstDayOfMonth(Integer year, Integer month) {
return LocalDate.of(year, month, 1);
}
public static LocalDate getLastDayOfMonth(Integer year, Month month) {
return LocalDate.of(year, month, 1).with(TemporalAdjusters.firstDayOfMonth());
}
public static LocalDate getLastDayOfMonth(Integer year, Integer month) {
return LocalDate.of(year, month, 1).with(TemporalAdjusters.firstDayOfMonth());
}
public static LocalDate getFirstDayOfYear(Integer year) {
return LocalDate.of(year, Month.JANUARY, 1);
}
public static LocalDate getLastDayOfYear(Integer year) {
return LocalDate.of(year, Month.DECEMBER, 31);
}
//Date 转换为 LocalDate
public static LocalDate toLocalDate(Date date){
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = date.toInstant();
return instant.atZone(zoneId).toLocalDate();
}
}