Java 8 to start now ~ Date time API ~

Last time: Continued Java8 ~ Stream API ~ Two times before: Java8 ~ forEach and lambda expression ~

Date-time API added in Java 8

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

Typical class


--Can both hold and manipulate datetime values --Time can be expressed with accuracy up to nanoseconds --Immutable

How to use

The usage of LocalDateTime, OffsetDateTime, and ZonedDateTime is almost the same, so I will explain with LocalDateTime.

Get the current time

LocalDateTime current =;  // -> 2018-03-03T12:09:50.875

Generated by specifying the date and time

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.

Conversion from Date / Calendar

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());

Get date and time

There is a method to get each year, month, day, hour, minute, second, nanosecond

LocalDateTime current =; // -> 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.

Date and time manipulation

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 =;     // -> 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 =;
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 =;
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

Java 8 to start now ~ Date time API ~
Java 8 ~ Stream API ~ to start now
Java8 to start now ~ Optional ~
[Java] Date / time operations
Java8 to start now ~ forEach and lambda expression ~
[Java] Introduction to Stream API
Handle Java 8 date and time API with Thymeleaf with Spring Boot
I want to use the Java 8 DateTime API slowly (now)
Sample code to parse date and time with Java SimpleDateFormat
Now is the time to get started with the Stream API
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
[MySQL] [java] Receive date and time
Java, interface to start from beginner
Java, arrays to start with beginners
I tried to summarize Java 8 now
Introduction to java for the first time # 2
Studying Java 8 (date API in java.time package)
Convert from java UTC time to JST time
[Java] How to measure program execution time
Try various Java Stream API methods (now)
How to get the date in java
[Java] How to get the current date and time and specify the display format
Two ways to start a thread in Java + @
Date and time
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Java Stream API
How to use Java API with lambda expression
Date and time acquisition method using DateAndTime API
[Java] Introduction to Java
The date time of java8 has been updated
Introduction to java
[Java] How to operate List using Stream API
[Java] How to use Calendar class and Date class
[Java] How to use compareTo method of Date class
Features that are likely to enter Java 10 for now
[Java] I want to calculate the difference from the date
Change date and time to Japanese notation in Rails
[For beginners] How to operate Stream API after Java 8
Java development for beginners to start from 1-Vol.1-eclipse setup
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
[Java] I tried to implement Yahoo API product search
Summary of Java communication API (2) How to use HttpUrlConnection
~ I tried to learn functional programming in Java now ~
[Java] Platforms to choose from for Java development starting now (2020)
[Java] How to start a new line with StringBuilder
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Pack API response (java)
[Java] Stream API / map
[Java] Connect to MySQL
Accurate time measurement (Java)
Docker-Client Java API Troubleshooting
Kotlin's improvements to Java
Java8 Stream API practice
From Java to Ruby !!
Zabbix API in Java
Review Java annotations now
Date manipulation in Java 8
A concise summary of Java 8 date / time APIs that are likely to be used frequently
Introduction to java command