The date class in java8 is related to LocalDate


LocalDate now =;
System.out.println(now);   // 2021-11-10

You can see that the print result is only month, year and day,

You can also construct a date with a given number

LocalDate date1 = LocalDate.of(2021,11,20);
LocalDate date2 = LocalDate.of(2021, Month.NOVEMBER,20);

The second parameter of the second method is to specify an enumeration. The results of the two printing are the same. Then this month should start from 1, not 0.

        LocalDate ld2 =, ChronoUnit.DAYS);
        LocalDate ld3 = date1.plusDays(1);

You can also add or subtract a few days from the date, or you can also operate on the week, month and year

        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
//        November 10, 2021

The last time I used Instant to get the month, year and day was not completed, but this is really easy.

        LocalDate after7day = now.plusDays(21);
        Long gap1 = now.until(after7day,ChronoUnit.DAYS);
//        21

You can also use the util method to calculate the interval gap between two dates and specify the unit. I tried to add 6 days to the original basis, and then when calculating the date interval, the second parameter of the util method is specified as chrononunit.weeks, and the calculation result is 0.


There are also methods to compare before and after.


You can see that this class should mainly look at hours, minutes and seconds.

        int hour = now.getHour();
        int minutes = now.getMinute();
        int second = now.getSecond();

Easily obtain the specific value of hours, minutes and seconds.

LocalTime lt1 = LocalTime.of(23,29,29);

You can also build an object with the specified value. Similarly, there are similar operation methods of minus hours, so I won't repeat them.


It can be seen from the print result that the date, hour, minute and second are all in line, and it is Beijing time.

There are many parameters for this static construction method.

        LocalDateTime ldt2 = LocalDateTime.of(2027,10,10,10,29);

If you do not specify the number of seconds, there will be no print result.

        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();

It is also convenient to directly obtain the value of each time unit. Similar plus and minus are also available.


     DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate date =;
        LocalTime time =;
        LocalDateTime dateTime =;
        System.out.println("date = "+date);
        System.out.println("time = "+time);
        System.out.println("dateTime = "+dateTime);
//        System.out.println(format.format(time)); Because time has only hours, minutes and seconds, it cannot be formatted

There is no problem formatting LocalDate and LocalDateTime directly.

However, if you change the format to DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss");
An error will be reported because there are no hours, minutes and seconds in LocalDate, and LocalDateTime can be formatted

DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy/MM/01"); //It's OK

DateTimeFormatter converts the string to LocalDate

        DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String s1 = "2021/12/13";
        LocalDate date = LocalDate.parse(s1,format);

DateTimeFormatter converts string to LocalDateTime

        DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s1 = "2021-10-10 20:09:18";

DateTimeFormatter converts string to LocalTime

DateTimeFormatter converts LocalDateTime to a string

        DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH-mm-ss");
        LocalDateTime now =;
        System.out.println(format.format(now)); //  2021/11/10 23-46-29

DateTimeFormatter converts LocalDate to a string

       DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate now =;

DateTimeFormat converts a date to a string

        DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH-mm-ss");
        LocalDateTime now =;
        System.out.println(now.format(format));// 2021/11/11 07-17-11

        DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy|MM|dd");
        LocalDate date =;

Tags: Java

Posted on Wed, 10 Nov 2021 18:24:19 -0500 by LTJason