1. 程式人生 > >java8 時間日期 LocalDate

java8 時間日期 LocalDate

新的時間及日期API位於java.time中

Instant——代表的是時間戳

LocalDate——不包含具體時間的日期,比如2014-01-14。它可以用來儲存生日,週年紀念日,入職日期等。

LocalTime——代表的是不含日期的時間

LocalDateTime——包含了日期及時間,不過還是沒有偏移資訊或者說時區。

ZonedDateTime——這是一個包含時區的完整的日期時間,偏移量是以UTC/格林威治時間為基準的。

1.LocalDate

LocalDate d = LocalDate.now();
LocalDate ofDate = LocalDate.of(2018, 11, 12);
LocalDate parseDate = LocalDate.parse("20181112", DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("today:" + d);
System.out.println("ofDate:" + ofDate);
System.out.println("parseDate:" + parseDate);
System.out.println("getDayOfYear:" + d.getDayOfYear());
System.out.println("getMonth:" + d.getMonth());
System.out.println("getDayOfMonth:" + d.getDayOfMonth());
System.out.println("getDayOfWeek:" + d.getDayOfWeek().getValue());
System.out.println("now vs ofDate:" + d.equals(ofDate));

today:2018-11-12
ofDate:2018-11-12
parseDate:2018-11-12
getDayOfYear:316
getMonth:NOVEMBER
getDayOfMonth:12
getDayOfWeek:1
now vs ofDate:true