The date and time common API [java8]

Java before 8 library is not ideal support for date and time, in order to solve this problem, Java 8 introduced a new date and time API, located in java.time path.

LocalDate

LocalDate type contains date information, use some LocalDate following give examples:

LocalDate date = LocalDate.of(2018, 4, 20); // 2018-04-20
int year = date.getYear(); // 2018
int month = date.getMonth().getValue(); // 4
int day = date.getDayOfMonth(); // 20
// 查看该月有多少天
int days = date.lengthOfMonth(); // 30
// 是否是闰年
boolean isLeap = date.isLeapYear(); // false
复制代码

You can use LocalDate.now () to obtain information about the day's date:

// 查看当天 年月日
LocalDate today = LocalDate.now(); // 2018-04-20
复制代码

In addition to calling LocalDate of getYear method, we can also use ChronoField enumerated types to achieve the same functionality:

int year1 = date.get(ChronoField.YEAR); // 2018
int month1 = date.get(ChronoField.MONTH_OF_YEAR); // 4
int day1 = date.get(ChronoField.DAY_OF_MONTH); // 20
// 当前日期属于该月第几周
int weekOfMonth = date.get(ChronoField.ALIGNED_WEEK_OF_MONTH); // 3
复制代码

ChronoField enumeration type contains a lot of properties to choose from:

We can also modify LocalDate objects:

LocalDate date3 = LocalDate.of(2018, 4, 20); // 2018-04-20
LocalDate date4 = date3.withDayOfMonth(22); // 2018-04-22
LocalDate date5 = date3.with(ChronoField.DAY_OF_MONTH, 22); // 2018-04-22
LocalDate date6 = date3.withYear(2019); // 2019-04-20
LocalDate date7 = date3.plusDays(5); // 2018-04-25
LocalDate date8 = date3.plus(5, ChronoUnit.DAYS); // 2018-04-25
LocalDate date9 = date3.minusYears(10); // 2008-04-20
复制代码

TemporalAdjusters class provides a number of static methods to modify LocalDate objects. When we need to get the next Sunday, the next working day, the last day of the month and other information, TemporalAdjusters class can come in handy:

import static java.time.temporal.TemporalAdjusters.*;
LocalDate date10 = date3.with(nextOrSame(DayOfWeek.MONDAY)); // 2018-04-23
LocalDate date11 = date3.with(lastDayOfMonth()); // 2018-04-30
LocalDate date12 = date3.with(previous(DayOfWeek.SATURDAY)); // 2018-04-14
复制代码

Refer to the April calendar to understand the above results:

We can also format operation LocalDate:

String str1 = date.format(DateTimeFormatter.BASIC_ISO_DATE); // 20180420
String str2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2018-04-20

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String str5 = date.format(dtf); // 2018-04-20
LocalDate date13 = LocalDate.parse(str5, dtf); // 2018-04-20
复制代码

We shared between many similar methods to be described below and LocalDate LocalTime, the LocalDateTime, LocalDate modifications described above, and other methods common format suitable for LocalTime and LocalDateTime.

LocalTime

LocalTime LocalDate and the like, except that LocalTime contains the minutes and seconds (ms) information. LocalTime give some examples:

LocalTime time = LocalTime.of(20, 13, 54); // 20:13:54
int hour = time.getHour(); // 20
int minute = time.getMinute(); // 13
int second = time.getSecond(); // 54
复制代码

LocalDate and LocalTime can be created by a string:

LocalDate date = LocalDate.parse("2018-04-20");
LocalTime time = LocalTime.parse("20:13:54");
复制代码

localdateti to

LocalDate and LocalTime LocalDateTime is a combination of forms, including the date when the information every minute. LocalDateTime give some examples of their use:

LocalDateTime ldt1 = LocalDateTime.of(2018, 4, 20, 20, 13, 54); // 2018-04-20T20:13:54
LocalDateTime ldt2 = LocalDateTime.of(date, time); // 2018-04-20T20:13:54
复制代码

LocalDateTime can be converted to LocalDate and LocalTime, the information contained in the converted reduced:

LocalDate date1 = ldt1.toLocalDate(); // 2018-04-20
LocalTime time1 = ldt1.toLocalTime(); // 20:13:54
复制代码

Same, LocalDate and LocalTime may be converted to LocalDateTime, just fill the date or time:

LocalDateTime ldt3 = date.atTime(time); // 2018-04-20T20:13:54
LocalDateTime ldt4 = date.atTime(20, 13, 54); // 2018-04-20T20:13:54
LocalDateTime ldt5 = time.atDate(date); // 2018-04-20T20:13:54
复制代码

Duration

Duration time for calculating the difference between the two LocalTime or LocalDateTime, for example:

LocalTime time2 = LocalTime.of(23, 59, 59);
Duration duration = Duration.between(time1, time2);
long seconds = duration.getSeconds(); // 13565
复制代码

The difference between 13,565 seconds time1 and time2.

Duration object created manually:

Duration threeMinutes = Duration.ofMinutes(3);
threeMinutes = Duration.of(3, ChronoUnit.MINUTES); // 创建了一个3分钟的Duration,两种创建方式等价
复制代码

Period

Period duration between the two used to calculate LocalDate. Give some examples:

LocalDate date2 = LocalDate.of(2018, 5, 21);
Period period = Period.between(date1, date2);
int monthsBetween = period.getMonths(); // 1
int daysBetween = period.getDays(); // 1
复制代码

1 month difference between 2018-04-21 and 2018-04-20, a difference of a few days.

Similarly, we can also manually create objects Period:

Period tenDays = Period.ofDays(10);
Period threeWeeks = Period.ofWeeks(3);
Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
复制代码

Some other commonly used methods

Compare two times successively

LocalDate date15 = LocalDate.of(2018,4,21);
date.isEqual(date15); // false
date.isAfter(date15); // false
date.isBefore(date15); // true
复制代码

Use MonthDay class

MonthDay contain only day of information, it can be used to store like birthdays, wedding anniversaries and other information. For example using MonthDay:

LocalDate birthday = LocalDate.of(1999, 9, 9);
MonthDay monthDay = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(LocalDate.now());
if (currentMonthDay.equals(monthDay)) {
    System.out.println("happy birthday!");
}
复制代码

If the user's birthday is September 9, 1999, it can be judged by whether this user's birthday is today, if it is, then it sends birthday wishes. The same is also YearMonth class.

Epilogue

Java 8 new date and time API also provides a method of acquiring time zone and a calendar, and not easy to use due to less appreciated, not listed here.

Guess you like

Origin juejin.im/post/5d5b6880f265da03da249701