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