The date class LocalDate

It can easily get the date of the day, and can easily add or subtract the date.

He creates objects using static methods or methods like from/of.

This class does not store time zones, so it does not have the concept of a time zone. If a time zone is needed, ZonedDateTime is used

This class can only perform date-based operations, not specific times.

Here are some common methods

atTime

Generates a date with a time, returns LocalDateTime so this is obviously a date class with a time.

LocalDateTime time = LocalDate.now().atTime(12.3.22.232);
System.out.println(time); / / the T12:2019-05-10 03:22. 000000232
Copy the code

compareTo()

The value returned by comparing two times is int. If the value is greater than 0, the comparator is larger; otherwise, the comparator is larger and 0 is equal.

format()

Format the time by passing a formatter of type [DateTimeFormatter].

parse()

The first argument is the parsed string, and the second argument is the formatter passed in

plus/minus

To add or subtract dates, the first argument is passed in as int and the second as units, using the ChronoUnit enumerated type

public class LocalDate1 {

    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        // Add time
        LocalDate i22day = today.plusDays(22);
        LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
        // Subtract time
        LocalDate s22day = today.minus(22, ChronoUnit.DAYS);
        LocalDate yesterday = tomorrow.minusDays(2);

        System.out.println(today);
        System.out.println(tomorrow);
        System.out.println(yesterday);
        System.out.println(i22day);
        System.out.println(s22day);

        LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
        DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
        System.out.println(dayOfWeek);    // FRIDAY

        DateTimeFormatter germanFormatter =
                DateTimeFormatter
                        .ofLocalizedDate(FormatStyle.MEDIUM)
                        .withLocale(Locale.GERMAN);

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/mm/dd hh:mm:ss");
        LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);
        // System.out.println(LocalDate.parse("2014/02/01 12:03:22", dateTimeFormatter));
        System.out.println(xmas);   / / 2014-12-24


        System.out.println(LocalDate.now().atStartOfDay());

        LocalDateTime time = LocalDate.now().atTime(12.3.22.232);
        System.out.println(time);
    }
Copy the code

Time LocalDateTime

This is a time class similar to the one above, not only date but also time.

atZone

Generate date with time zone

format

Formatting time

of

Create object from a time of(int year, int month, int dayOfMonth, int hour, int minute, int second)

parse

** Parse **([CharSequence]text, [DateTimeFormatter]

public class LocalDateTime1 {

    public static void main(String[] args) {

        LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31.23.59.59);

        DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
        System.out.println(dayOfWeek);      // WEDNESDAY

        Month month = sylvester.getMonth();
        System.out.println(month);          // DECEMBER

        long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
        System.out.println(minuteOfDay);    / / 1439

        Instant instant = sylvester
                .atZone(ZoneId.systemDefault())
                .toInstant();

        Date legacyDate = Date.from(instant);
        System.out.println(legacyDate);     // Wed Dec 31 23:59:59 CET 2014


        DateTimeFormatter formatter =
                DateTimeFormatter
                        .ofPattern("MMM dd, yyyy - HH:mm");

        LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);
        String string = parsed.format(formatter);
        System.out.println(string);     // Nov 03, 2014 - 07:13}}Copy the code

LocalTime LocalTime

This class is the Time part of LocaDateTime above, which has only the Time and no date.

public class LocalDate1 { public static void main(String[] args) { LocalDate today = LocalDate.now(); // Add time LocalDate i22day = today.plusdays (22); LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); // subtract LocalDate s22day = today.minus(22, chronounit.days); // Subtract LocalDate s22day = today.minus(22, chronounit.days); LocalDate yesterday = tomorrow.minusDays(2); System.out.println(today); System.out.println(tomorrow); System.out.println(yesterday); System.out.println(i22day); System.out.println(s22day); LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); System.out.println(dayOfWeek); // FRIDAY DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedDate(FormatStyle.MEDIUM) .withLocale(Locale.GERMAN); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/mm/dd hh:mm:ss"); Parse ("24.12.2014", germanFormatter); // System.out.println(LocalDate.parse("2014/02/01 12:03:22", dateTimeFormatter)); System.out.println(xmas); // 2014-12-24 System.out.println(LocalDate.now().atStartOfDay()); LocalDateTime time = localdate.now ().attime (12, 3, 22,232); System.out.println(time); }Copy the code

The Clock Clock

This class can get the current timestamp

public class Clock1 {
    public static void main(String[] args) {
        Clock millis = Clock.tickMillis(ZoneOffset.UTC);
        Clock seconds = Clock.tickSeconds(ZoneOffset.UTC);
        Clock minutes = Clock.tickMinutes(ZoneOffset.UTC);
        // Time stamp to the millisecond
        System.out.println(millis.millis());
        // Time stamp accurate to the second
        System.out.println(seconds.millis());
        // The timestamp is accurate to the minute
        System.out.println(minutes.millis());
        // Accurate to the millisecond
        System.out.println(millis.instant());
        // Accurate to the second time
        System.out.println(seconds.instant());
        // Accurate to the minuteSystem.out.println(minutes.instant()); }}Copy the code

The output above looks like this:

2019-05-11t9:35:39z 2019-05-11t9:35:39z 2019-05-11t9:35:39z 2019-05-11t9:35:35:00 ZCopy the code

Map initialization tips

The ZoneId interface of the Clock class uses a Map and initializes the Map. Generally, data must be put for initialization, so ofEntries are used to initialize multiple groups of data.

public static final Map<String, String> SHORT_IDS = Map.ofEntries(
        entry("ACT"."Australia/Darwin"),
        entry("AET"."Australia/Sydney"),
        entry("AGT"."America/Argentina/Buenos_Aires"),
        entry("ART"."Africa/Cairo"),
        entry("AST"."America/Anchorage"),
        entry("BET"."America/Sao_Paulo"),
        entry("BST"."Asia/Dhaka"),
        entry("CAT"."Africa/Harare"),
        entry("CNT"."America/St_Johns"),
        entry("CST"."America/Chicago"),
        entry("CTT"."Asia/Shanghai"),
        entry("EAT"."Africa/Addis_Ababa"),
        entry("ECT"."Europe/Paris"),
        entry("IET"."America/Indiana/Indianapolis"),
        entry("IST"."Asia/Kolkata"),
        entry("JST"."Asia/Tokyo"),
        entry("MIT"."Pacific/Apia"),
        entry("NET"."Asia/Yerevan"),
        entry("NST"."Pacific/Auckland"),
        entry("PLT"."Asia/Karachi"),
        entry("PNT"."America/Phoenix"),
        entry("PRT"."America/Puerto_Rico"),
        entry("PST"."America/Los_Angeles"),
        entry("SST"."Pacific/Guadalcanal"),
        entry("VST"."Asia/Ho_Chi_Minh"),
        entry("EST"."- 05:00"),
        entry("MST"."-07:00"),
        entry("HST"."- 10:00." "));Copy the code