“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

LocalTime

Creating an object is similar to localDate

LocalTime time = LocalTime.now(); / / 14:59:51. 86Copy the code

The above three of specify the creation time as required

LocalTime of = LocalTime.of(14, 59, 51, 333); //14:59:51.000000333 LocalTime of1 = LocalTime. Of (14, 59, 51); / / 14:59:51Copy the code

Increase/decrease

  • plusHours
  • plusMinutes
  • plusSeconds
  • plusNanos

To compare

CompareTo () returns 1 if greater than, -1 if less than

LocalTime of = LocalTime.of(14, 59, 51, 333); //14:59:51.000000333 LocalTime of1 = LocalTime. Of (14, 59, 51); // 14:59:51 int i3 = of.compareTo(of1); Public int compareTo(LocalTime other) {int CMP = Integer.compare(hour, other.hour); if (cmp == 0) { cmp = Integer.compare(minute, other.minute); if (cmp == 0) { cmp = Integer.compare(second, other.second); if (cmp == 0) { cmp = Integer.compare(nano, other.nano); } } } return cmp; }Copy the code

LocalDateTime

create

Now () creates an object of the current time, connected by T between the date and time

LocalDateTime dateTime = LocalDateTime.now(); / / the 2021-11-09 T09:22:05. 526Copy the code

Of () manually constructed

1. Manually specify the year, month, day, hour, minute, and second. When the following arguments default, they are set to 0

// 2021-11-11T11:11:11
LocalDateTime of2 = LocalDateTime.of(2021, 11, 11, 11, 11, 11);  
Copy the code

2. The parameters are LocalDate and LocalTime. Concatenate the two parameters into LocalDateTime

// localDate: 21-12-08 LocalDateTime of3 = LocalDateTime. Of (localDate, time); / / T16 2021-12-08:02:30. 375Copy the code

Convert to the other two classes

LocalDateTime has two properties, date and time, which store the date and time, respectively. So converting to and from the other two classes is particularly convenient

		private final LocalDate date;
    private final LocalTime time;
Copy the code

ToLocalDate () returns the date property directly

LocalDate localDate2 = of3.toLocalDate();
Copy the code

ToLocalTime () returns the time attribute directly

LocalTime localTime = of3.toLocalTime();
Copy the code

To obtain

Because there are two attributes, so LocalDate and LocalTime

The parse analytical

You can convert strings directly to the corresponding Local class.

1. Use the default parsing format, just the string with the time

	
				LocalDate parse = LocalDate.parse("2021-08-09");
        LocalTime parse1 = LocalTime.parse("23:59:59");
				LocalDateTime parse2 = LocalDateTime.parse("2021-08-09T23:59:59");

			
Copy the code

Source:

Public static LocalDateTime parse(CharSequence text) DateTimeFormatter formatter) { Objects.requireNonNull(formatter, "formatter"); return formatter.parse(text, LocalDateTime::from); Public static final DateTimeFormatter ISO_LOCAL_DATE_TIME public static final DateTimeFormatter ISO_LOCAL_DATE_TIME; static { ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder() .parseCaseInsensitive() .append(ISO_LOCAL_DATE) .appendLiteral('T') .append(ISO_LOCAL_TIME) .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); }Copy the code