The date class in java8 is related to LocalDate

LocalDate

LocalDate now = 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 = date1.plus(1, 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

        System.out.println(date1.minusDays(1));
        System.out.println(date1.plusWeeks(1));
        System.out.println(date1.minusWeeks(1));
        System.out.println(date1.plusMonths(1));
        System.out.println(date1.minusMonths(1));
        System.out.println(date1.plusYears(1));
        System.out.println(date1.minusYears(1));
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        System.out.println(year+"year"+month+"month"+day+"day");
//        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);
        System.out.println(gap1);
//        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.

        System.out.println(after7day.isAfter(now));//true
        System.out.println(now.isBefore(after7day));//true

There are also methods to compare before and after.

LocalTime


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.

LocalDateTime


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);
        System.out.println(ldt2);//2027-10-10T10: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();
        System.out.println(year+"+"+month+"+"+day+"+"+hour+"+"+minute+"+"+second);//2021+11+10+22+58+33

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

DateTimeFormatter

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

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";
        System.out.println(LocalDateTime.parse(s1,format));

DateTimeFormatter converts string to LocalTime

DateTimeFormatter converts LocalDateTime to a string

        DateTimeFormatter  format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH-mm-ss");
        LocalDateTime now = 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 = LocalDate.now();
        System.out.println(format.format(now));//2021/11/10

DateTimeFormat converts a date to a string

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

        DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy|MM|dd");
        LocalDate date = LocalDate.now();
        System.out.println(date.format(format1));//2021|11|11

Tags: Java

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