localDate
java.util.Date is generally used for time processing in java, but compared to Date, there is a better choice -- java.time.LocalDate.
This is a new date processing class in jdk8, as well as java.time.LocalTime, java.time.LocalDateTime, etc.
LocalDate does not contain time, just simple year, month and day. To be accurate to hours, minutes, seconds or even milliseconds, you need to use java.time.LocalDateTime.
Methods as below:
getYear() int Get the year of the current date getMonth() Month Gets the month object of the current date getMonthValue() int Get the month of the current date getDayOfWeek() DayOfWeek indicates that the date represented by the object is the day of the week getDayOfMonth() int indicates that the date represented by the object is the day of the month getDayOfYear() int indicates that the date represented by this object is the day of the year withYear(int year) LocalDate Modify the year of the current object withMonth(int month) LocalDate Modify the month of the current object withDayOfMonth(int dayOfMonth) LocalDate Modify the date of the current object in the current month isLeapYear() boolean whether it is a leap year lengthOfMonth() int how many days this month has lengthOfYear() int The number of days in the year represented by the object (365 or 366) plusYears(long yearsToAdd) LocalDate The current object is incremented by the specified number of years plusMonths(long monthsToAdd) LocalDate The current object is incremented by the specified number of months plusWeeks(long weeksToAdd) LocalDate The current object is added by the specified number of weeks plusDays(long daysToAdd) LocalDate The current object is added by the specified number of days minusYears(long yearsToSubtract) LocalDate The current object minus the specified number of years minusMonths(long monthsToSubtract) LocalDate Current object minus the number of months destined minusWeeks(long weeksToSubtract) LocalDate The current object minus the specified number of weeks minusDays(long daysToSubtract) LocalDate The current object minus the specified number of days compareTo(ChronoLocalDate other) int Compare the size of the current object and the other object in time, if the return value is positive, the current object time is later, isBefore(ChronoLocalDate other) boolean Compare whether the current object date is before the other object date isAfter(ChronoLocalDate other) boolean Compare whether the current object date is after the other object date isEqual(ChronoLocalDate other) boolean Compares two date objects for equality
The general usage is as follows:
// Get the current date: LocalDate today = LocalDate.now(); // -> 2018-04-02 // Get the date according to the year, month and day: LocalDate crischristmas = LocalDate.of(2014, 12, 25); // -> 2014-12-25 // Take from the string: LocalDate endOfFeb = LocalDate.parse("2014-02-28"); // Strictly according to ISO yyyy-MM-dd verification, 02 cannot be written as 2, of course, there is also an overloaded method that allows you to define the format yourself // Get the 1st day of the month: LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2018-04-01 // Get the 2nd day of the month: LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2018-04-02 // Take the last day of the month, no longer need to calculate whether it is 28, 29, 30 or 31: LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2018-04-30 // Get the next day: LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1); // becomes 2018-05-01 // Take the first Monday of January 2017, using Calendar will kill a lot of brain cells: LocalDate firstMondayOf2015 = LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2017-01-02
LocalTime
LocalTime contains only time, get the current time:
LocalTime now = LocalTime.now(); // 23:11:08.006
Construction time:
LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00 LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00
You can access its hours, minutes, seconds, nanoseconds via these methods:
getHour() getMinute() getSecond () getNano ()
The LocalTime class contains a series of methods that can help you perform time calculations:
plusHours() plusMinutes () plusSeconds() plusNanos () minusHours () minusMinutes () minusSeconds () minusNanos()
LocalDateTime
Create a LocatDateTime instance
You can create a LocalDateTime instance through the LocalDateTime static factory method. The following example uses the now() method to create: LocalDateTime localDateTime = LocalDateTime.now();
Another way is to use the specified year, month, day, hour, minute, second, and nanosecond to create a new object: LocalDateTime localDateTime2 = LocalDateTime.of(2015, 11, 26, 13, 55, 36, 123);
You can access its datetime via these methods:
getYear() getMonth() getDayOfMonth() getDayOfWeek() getDayOfYear() getHour() getMinute() getSecond () getNano ()
The LocalDateTime class contains a series of methods that can help you perform time calculations:
plusYears() plusMonths() plusDays() plusHours() plusMinutes () plusSeconds() plusNanos () minusYears () minusMonths() minusDays () minusHours () minusMinutes () minusSeconds () minusNanos()
Reference: https://www.cnblogs.com/jtools/p/6502999.html
https://blog.csdn.net/tjgykhulj/article/details/69053938
https://blog.csdn.net/tjgykhulj/article/details/69053940