A concise summary of Java 8 date / time APIs that are likely to be used frequently

I've briefly summarized only the things that are likely to be used frequently in practice regarding the Java 8 date / time API.

java.time.LocalDate class

Reference

LocalDate a = LocalDate.now();
System.out.println(a); // YYYY-MM-DD

LocalDate b = LocalDate.of(2017, 10, 17);
System.out.println(b); // 2017-10-17

LocalDate c = LocalDate.parse("2017-10-17");
System.out.println(c); // 2017-10-17
System.out.println(c.getYear()); // 2017
System.out.println(c.getMonthValue()); // 10
System.out.println(c.getDayOfMonth()); // 17
System.out.println(c.getDayOfWeek()); // "TUESDAY"
System.out.println(c.getDayOfWeek().getValue()); // 2

LocalDate d = c.plusDays(30);
System.out.println(c); // 2017-10-17 * Since it is immutable, the original value does not change.
System.out.println(d); // 2017-11-16

java.time.LocalTime class

Reference

--Local Time is immutable --Handling time in 24 hours, no distinction between morning and afternoon

LocalTime a = LocalTime.of(0, 1, 2);
System.out.println(a); // 00:01:02

LocalTime b = a.plusHours(12);
System.out.println(a); // 00:01:02 * Since it is immutable, the original value does not change.
System.out.println(b); // 12:01:02

java.time.LocalDateTime class

Reference

--Basic specifications are the same as LocalDate / LocalTime

java.time.format.DateTimeFormatter class

Reference

--Use both LocalDate / LocalTime / LocalDateTime in the format method --The constant (formatter) defined in the DateTimeFormatter class can be used as an argument.

Formatter Example
BASIC_ISO_DATE 20171017
ISO_LOCAL_DATE 2017-10-17
ISO_LOCAL_TIME 10:15:30
LocalDateTime dateTime = LocalDateTime.of(2017, 10, 17, 10, 15, 30);
System.out.println(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE)); // 20171017
System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE)); // 2017-10-17
System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME)); // 10:15:30

java.time.Duration class

Reference

--You can get the date difference and time difference.

LocalDateTime start = LocalDateTime.of(2017, 10, 17, 0, 0);
LocalDateTime end = LocalDateTime.of(2017, 11, 17, 0, 0);
Duration d = Duration.between(start, end);
System.out.println(d.toDays()); //The 31st)
System.out.println(d.toHours()); //744 (hours)

** (Reference & Caution) ** Java 8 also has an API called ** java.time.Period class ** to get the time period. However, this API asks for the period in the format of "○ years, ○ months, and ○ days". If you use it for the purpose of acquiring the so-called "days", it will be different from the expected result, so you must be careful not to misunderstand it.

LocalDate start = LocalDate.of(2017, 10, 17);
LocalDate end = LocalDate.of(2017, 11, 17);
Period p = Period.between(start, end);
System.out.println(p.getMonths()); //1 month
System.out.println(p.getDays()); //0th (Note) Bug if the expected result is assumed to be "31st"

Recommended Posts

A concise summary of Java 8 date / time APIs that are likely to be used frequently
Summary of ORM "uroboroSQL" that can be used in enterprise Java
A collection of RSpecs that I used frequently
10 barrages of drawing with ● or ■ that are likely to appear in training (Java)
java stream A memorandum of intermediate / termination operations that are really often used 1
Features that are likely to enter Java 10 for now
A collection of commands that were frequently used on heroku
[Personal memo] Frequently used Java grammar updated from time to time
Java 14 new features that could be used to write code
How to make a key pair of ecdsa in a format that can be read by Java
Summary of frequently used Docker commands
A memo that enabled VS Code + JUnit 5 to be used on Windows 10
How to make a Java calendar Summary
[java] Summary of how to handle char
[Java] How to set the Date time to 00:00:00
Java 8 to start now ~ Date time API ~
Java Summary of frequently searched type conversions
[Android] I want to create a ViewPager that can be used for tutorials
Java String class methods that are a little useful to know but don't know
[java] Summary of how to handle character strings
Things to be aware of when writing Java
A story that took time to establish a connection
[Java] Summary of how to abbreviate lambda expressions
Set the time of LocalDateTime to a specific time
[Updated from time to time] Links that are indebted
The date time of java8 has been updated
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
[Java] The problem that uploaded images are not updated due to the influence of cache
Installing Java is not easy! Summary of precautions to finish perfectly in one time
I'll forget to look it up, so I've put together a list of frequently used Docker commands [updated from time to time]