By LocalDate source code analysis Java API Date Time Series 8 ----- Jdk8 new date and time API class java.time package , it can be seen java8 design is very good, implement the interface Temporal, TemporalAdjuster, ChronoLocalDate, etc., we have a very rich way. For example: the LocalDateTime: Method part:
It contains the acquisition date, minutes and seconds nanoseconds. Date if you want to get this information, you must use the Calendar can. Now thread-safe acquisition date, hour, minute, and other information by Date converted to LocalDateTime, it can be very convenient.
public static int getYear(Date date){ return DateTimeConverterUtil.toLocalDateTime(date).getYear(); } public static int getYear(Instant instant){ return DateTimeConverterUtil.toLocalDateTime(instant).getYear(); } public static int getMonth(Date date){ return DateTimeConverterUtil.toLocalDateTime(date).getMonthValue(); } public static int getMonth(Instant instant){ return DateTimeConverterUtil.toLocalDateTime(instant).getMonthValue(); } public static int getDayOfMonth(Date date){ return DateTimeConverterUtil.toLocalDateTime(date).getDayOfMonth(); } public static int getDayOfMonth(Instant instant){ return DateTimeConverterUtil.toLocalDateTime(instant).getDayOfMonth(); } public static int getHour(Date date){ return DateTimeConverterUtil.toLocalDateTime(date).getHour(); } public static int getHour(Instant instant){ return DateTimeConverterUtil.toLocalDateTime(instant).getHour(); } public static int getMinute(Date date){ return DateTimeConverterUtil.toLocalDateTime(date).getMinute(); } public static int getMinute(Instant instant){ return DateTimeConverterUtil.toLocalDateTime(instant).getMinute(); } public static int getSecond(Date date){ return DateTimeConverterUtil.toLocalDateTime(date).getSecond(); } public static int getSecond(Instant instant){ return DateTimeConverterUtil.toLocalDateTime(instant).getSecond(); }
Test categories:
@Test public void dateCalculatorGetTest(){ Date date = new Date(); System.out.println(date); System.out.println(DateTimeConverterUtil.toLocalDateTime(date)); System.out.println(DateTimeCalculatorUtil.getYear(date)); System.out.println(DateTimeCalculatorUtil.getMonth(date)); System.out.println(DateTimeCalculatorUtil.getDayOfMonth(date)); System.out.println(DateTimeCalculatorUtil.getHour(date)); System.out.println(DateTimeCalculatorUtil.getMinute(date)); System.out.println(DateTimeCalculatorUtil.getSecond(date)); }
Output:
Sun Jan 12 22:24:07 CST 2020 2020-01-12T22:24:07.681 2020 1 12 22 24 7
Source address: https://github.com/xkzhangsan/xk-time