Java8 LocalDate用法

  • 184
  • 0
  • 2016-07-12

Java8加入了LocalDate,在使用上比Calendar更好用,寫法上也更簡潔

取得該月的第一天和最後一天

LocalDate now = LocalDate.now();
LocalDate firstDayOfMonth = now.withDayOfMonth(1);  //當月的第一天
LocalDate endDayOfMonth = now.withDayOfMonth(startDate.lengthOfMonth()); //當月的最後一天

將LocalDate轉成Date型態

Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())