Studying Java 8 (date API in java.time package)

Studying the date API in the java.time package. I tried various things appropriately. The following sources.

package java8.dateapi;

import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * java.Studying the date API of the time package
 * @author komikcomik
 *
 */
public class DateApiStudy {

	public static void main(String[] args) {

		/*Try to get the current time for the time being*/
		System.out.println("---------------------Verification 1 Appropriate display using the current time-----------------------");
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Output for the time being [" + zonedDateTime + "】"); //Output without thinking

		/*Year, month, day, hour, minute, second, millisecond, etc.*/
		System.out.println("Year 【" + zonedDateTime.getYear() + "】");
		System.out.println("Month 【" + zonedDateTime.getMonthValue() + "】"); // getMonth()Note that OCTOBER will be returned.
		System.out.println("Day 【" + zonedDateTime.getDayOfMonth() + "】");
		System.out.println("Time 【" + zonedDateTime.getHour() + "】");
		System.out.println("Minutes [" + zonedDateTime.getMinute() + "】");
		System.out.println("Seconds [" + zonedDateTime.getSecond() + "】");
		System.out.println("Milliseconds or less [" + zonedDateTime.getNano() + "】");
		System.out.println("Only milliseconds [" + zonedDateTime.getLong(ChronoField.MILLI_OF_SECOND) + "】");
		System.out.println("If you want to get something like UTC + time [" + zonedDateTime.getOffset() + "】");

		/*Make a specified time*/
		System.out.println("---------------------Verification 2 Make a specified time-----------------------");
		ZonedDateTime shiteiTime = ZonedDateTime.of(2017, 10, 19, 16, 58, 59, 123456789, ZoneId.systemDefault());
		System.out.println("Output 2 for the time being [" + shiteiTime + "】"); //Output without thinking

		/*Convert to standard String*/
		System.out.println("---------------------Verification 3-----------------------");
		DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS");
		DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.nnnnnnnnn");
		System.out.println("Shape to milliseconds and put out [" + shiteiTime.format(dtf1) + "】");
		System.out.println("Shape up to nanoseconds and put out [" + shiteiTime.format(dtf2) + "】");

		/*Convert from String to LocalDateTime object*/
		System.out.println("---------------------Validation 4 Convert from String to LocalDateTime-----------------------");
		String dateStr = "2017/10/19 16:58:59.123456789";
		LocalDateTime ldt = LocalDateTime.parse(dateStr, dtf2);
		if (ldt.equals(shiteiTime.toLocalDateTime())) {
			System.out.println("Matches what was converted from String to LocalDateTime=" + ldt);
		} else {
			System.out.println("Does not match what was converted from String to LocalDateTime");
		}

		/*Convert from String to ZonedDateTime object*/
		System.out.println("---------------------Validation 5 Convert from String to ZonedDateTime-----------------------");
		String dateStrWithTimeZone = "2017/10/19 16:58:59.123456789 JST";
		DateTimeFormatter dtfWithTimeZone = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.nnnnnnnnn zzz");
		ZonedDateTime zdt = ZonedDateTime.parse(dateStrWithTimeZone, dtfWithTimeZone);
		if (zdt.equals(shiteiTime)) {
			System.out.println("Matches what was converted from String to ZonedDateTime=" + zdt);
		} else {
			System.out.println("Does not match what was converted from String to ZonedDateTime");
		}

		/* java.util.Convert to Date*/
		System.out.println("---------------------Validation 5 java.util.Convert to Date-----------------------");
		Date date = Date.from(shiteiTime.toInstant());
		System.out.println(date);

		/*Calculate at the end of the month*/
		System.out.println("---------------------Verification 6 Year Month calculates the end of the month-----------------------");
		YearMonth ym = YearMonth.from(ldt);
		System.out.println("End of month=" + ym.atEndOfMonth());
		ym = YearMonth.from(ldt.minusMonths(1));
		System.out.println("End of last month=" + ym.atEndOfMonth());
		ym = YearMonth.from(ldt.plusMonths(1));
		System.out.println("End of next month=" + ym.atEndOfMonth());

		System.out.println("---------------------Verification 7 Calculate the beginning and end of the month with Temporal Adjusters-----------------------");
		System.out.println(ldt.with(TemporalAdjusters.firstDayOfMonth()));
		System.out.println(ldt.with(TemporalAdjusters.lastDayOfMonth()));
	}
}

Below, the execution result


---------------------Verification 1 Appropriate display using the current time-----------------------
Output for the time being [2017-10-19T17:59:27.773+09:00[Asia/Tokyo]】
Year [2017]
Month [10]
Sun [19]
Time [17]
Minute [59]
Seconds [27]
Milliseconds or less [773000000]
Only milliseconds [773]
If you want to get something like UTC + time [+09:00】
---------------------Verification 2 Make a specified time-----------------------
Output 2 for the time being [2017-10-19T16:58:59.123456789+09:00[Asia/Tokyo]】
---------------------Verification 3-----------------------
Shape and put out to milliseconds [2017/10/19 16:58:59.123】
Shaped to nanoseconds and put out [2017/10/19 16:58:59.123456789】
---------------------Validation 4 Convert from String to LocalDateTime-----------------------
Matches what was converted from String to LocalDateTime=2017-10-19T16:58:59.123456789
---------------------Validation 5 Convert from String to ZonedDateTime-----------------------
Matches what was converted from String to ZonedDateTime=2017-10-19T16:58:59.123456789+09:00[Asia/Tokyo]
---------------------Validation 5 java.util.Convert to Date-----------------------
Thu Oct 19 16:58:59 JST 2017
---------------------Verification 6 Year Month calculates the end of the month-----------------------
End of month=2017-10-31
End of last month=2017-09-30
End of next month=2017-11-30
---------------------Verification 7 Calculate the beginning and end of the month with Temporal Adjusters-----------------------
2017-10-01T16:58:59.123456789
2017-10-31T16:58:59.123456789

When registering data in the date system column of DB, I often map it to java.util.Date with myBatis, but I would like to try next time if it can be mapped with ZonedDateTime or LocalDateTime.

Recommended Posts

Studying Java 8 (date API in java.time package)
Zabbix API in Java
Date manipulation in Java 8
Java Stream API in 5 minutes
View current date in Java
[Java] "T" is included in date type JSON in API response
Date processing in Java (LocalDate: Initialization)
Generate CloudStack API URL in Java
Hit Zaim's API (OAuth 1.0) in Java
Parsing the COTOHA API in Java
JPA (Java Persistence API) in Eclipse
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
I tried using Elasticsearch API in Java
How to implement date calculation in Java
Try using the Stream API in Java
Call the Windows Notification API in Java
Try using JSON format API in Java
Output Date in Java in ISO 8601 extended format
Java 8 to start now ~ Date time API ~
How to get the date in java
ChatWork4j for using the ChatWork API in Java
Partization in Java
[Java] API creation using Jerjey (Jax-rs) in eclipse
Changes in Java 11
Studying Java 8 (Optional)
Rock-paper-scissors in Java
Try using GCP's Cloud Vision API in Java
Java Stream API
Studying java9 (jShell)
Studying Java 8 (Stream)
Pi in Java
Try using the COTOHA API parsing in Java
FizzBuzz in Java
I tried Mastodon's Toot and Streaming API in Java
Call Amazon Product Advertising API 5.0 (PA-API v5) in Java
I tried using Google Cloud Vision API in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Pack API response (java)
Callable Interface in Java
Studying Java 8 (Collector / Collectors)
[Java] Package for management
[Java] Date / time operations
Use java.time in Jackson