Last time: Continued Java8 ~ Stream API ~ Two times before: Java8 ~ forEach and lambda expression ~
When handling date and time in Java7, value retention is separated from Date class and operation is separated from Calendar class, but API group that can be handled collectively in Java8 has been added
LocalDateTime Date time without time zone
OffsetDateTime Date and time with offset from UTC / Greenwich
ZonedDateTime Date / time with time zone
Instant A value that represents a certain time 1970-01-01T 00:00:00 It has a long that represents epoch seconds from Z and an int that represents nanoseconds.
--Can both hold and manipulate datetime values --Time can be expressed with accuracy up to nanoseconds --Immutable
The usage of LocalDateTime, OffsetDateTime, and ZonedDateTime is almost the same, so I will explain with LocalDateTime.
LocalDateTime current = LocalDateTime.now(); // -> 2018-03-03T12:09:50.875
LocalDateTime atMin = LocalDateTime.of(2018,3,3,12,1); // -> 2018-03-03T12:01
LocalDateTime atSec = LocalDateTime.of(2018,3,3,12,1,23); // -> 2018-03-03T12:01:23
LocalDateTime atNanoSec = LocalDateTime.of(2018,3,3,12,1,23,123456789); // -> 2018-03-03T12:01:23.123456789
There is a static method to generate by specifying the year, month, day, hour, minute, second, and nanosecond. Fields not specified will be zero The month can be specified by 1 to 12, unlike the Calendar class.
To convert from Date / Calendar to LocalDateTime, use the newly added .toInstant ()
method and go through Instant.
Get the system default because you need to specify the ZoneId
Calendar now = Calendar.getInstance();
Instant nowInstant = now.toInstant();
LocalDateTime fromCalendar = LocalDateTime.ofInstant(nowInstant, ZoneId.systemDefault());
There is a method to get each year, month, day, hour, minute, second, nanosecond
LocalDateTime current = LocalDateTime.now(); // -> 2018-03-03T12:09:50.875
System.out.println(current.Hour()); // -> 12
System.out.println(current.getMonth()); // -> MARCH
System.out.println(current.getMonthValue()); // -> 3
The getMonth ()
method returns an enum Month that represents the month.
Operations that the Calendar class was in charge of until Java 7 can be handled directly with LocalDateTime. There is a method that returns LocalDateTime added (plus) and subtracted (minus) for each year, month, day, hour, minute, second, and nanosecond. ** The value of the original instance does not change **
LocalDateTime current = LocalDateTime.now(); // -> 2018-03-03T12:09:50.875
LocalDateTime plusMin = current.plusMinutes(10); // -> 2018-03-03T12:19:50.875
LocalDateTime plusMonth = current.plusMonths(2); // -> 2018-05-03T12:09:50.875
LocalDateTime minusYear = current.minusYears(1); // -> 2017-03-03T12:09:50.875
Use the ʻofPattern method of the
DateTimeFormatter` class to format the date and time.
LocalDateTime current = LocalDateTime.now();
System.out.println(current.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS"))); // -> 2018/03/03 12:09:50.875
You can also use constants defined in ISO format
LocalDateTime current = LocalDateTime.now();
System.out.println(current.format(DateTimeFormatter.ISO_DATE)); // -> 2018-03-03
System.out.println(current.format(DateTimeFormatter.ISO_LOCAL_TIME)); // -> 12:09:50.875
Recommended Posts