Summary of Java8 processing dates
Java8 has made improvements to Lambda and Streams, and introduced a new processing time API to clarify the concept of time, such as: Instant, duration, date, time, time zone, and cycle. Unlike the old version, the new API is based on the IOS standard calendar system, and all classes in Java.time are thread-safe.
Key to the API
- ZonedDateTime: The most complete date and time, including UTC and time zone and Greenwich.
- LocalDateTime: excludes the time difference and time zone.
- LocalTime: contains time, but no date.
- LocalDate: indicates the LocalDate, excluding the specific time.
- Instant: indicates an Instant instance.
Code implementation
Projects are the most effective way to learn new things, mainly through simple code examples to access the time API in school Java8.
Use LocalDate to obtain the information about the current year, month, and day
LocalDate Indicates the current local time, but contains only the date and no time
/ * *
* Get the current time and date information
* /
public static void getNowDate() {
// Get the date only
LocalDate now = LocalDate.now();
System.out.println("Local time only :"+now);
Date date = new Date();
System.out.println("Java.util. Date fetch time :"+date);
int year = now.getYear();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
System.out.printf("Year: %d month: %d day: %d t %n",year,monthValue,dayOfMonth);
}
Copy the code
The results of
Local time only :2021-01-26
Date :Tue Jan 26 21:33:07 CST 2021
Year: 2021:1:26 T
Copy the code
Use LocalDate to get a specific time
Localdate.of () can create any time, just by entering the year, month and day argument, unlike the previous Java API, which started at 1900 and started at 0.
/ * *
* Processing time of features according to year, month and day
* @param year
* @param month
* @param day
* /
public static void setSpecilDate(int year,int month,int day) {
LocalDate date = LocalDate.of(year, month, day);
System.out.println("Set time to :"+date);
}
Copy the code
The results of
Set the time to 2021-1-26
Copy the code
Use equals to determine whether two dates are equal
In a real world project where a process would have to determine whether two dates are equal, LocalDate overrides the equal method so that they can be compared
/ * *
* Check whether two dates are equals
* /
public static void compareLocalDate() {
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2020, 12, 26);
boolean equals = now.equals(date);
String result =equals?"Equal":"Unequal";
System.out.printf("Time now %s and date %s %s \n", now, date,result);
}
Copy the code
The results of
Date now 2021-1-26 = date 2021-1-26
Copy the code
Use MonthDay to check the annual periodic time
In real projects, we often run into periodic events, such as birthdays, wedding anniversaries, and sending monthly emails to customers. In Java8, the MonthDay class is used to combine months and days so that we can determine annual events
/ * *
* Deal with things that only happen once a year
* /
public static void cycleDate() {
// Get the current time
LocalDate now = LocalDate.now();
// Set the characteristic time
LocalDate date = LocalDate.of(2020, 1, 26);
// Get the month and date
MonthDay monthDay = MonthDay.of(date.getMonth(), date.getDayOfMonth());
// Get the month and date of the current time
MonthDay currentDay = MonthDay.from(now);
String equals = currentDay.equals(monthDay)?"Yes":"Not";
System.out.printf("Time now %s and time date %s %s cycle date \n", now, date,equals);
}
Copy the code
The results of
Date now 2021-01-26 and Date 2020-01-26 are cycle dates
Copy the code
Use LocalTime to get the current time (only time without year, month and day). Time has been added or subtracted
LocalTime instead uses the static factory method localtime.now () to get the current time. The default format is HH :mm:ss:nm.
/ * *
* Add and subtract time
* /
public static void operatDate() {
LocalTime localTime = LocalTime.now();
LocalTime time = localTime.plusHours(-1).plusMinutes(1).plusSeconds(30);
System.out.printf("The current time is %s and %s \n after the addition or subtraction operation", localTime, time);
}
Copy the code
The results of
The current time is 22:03:52.522 and 21:05:22.522 after the addition and subtraction operations
Copy the code
Calculate the date N weeks, N months, or N years ago or later
/ * *
* Add and subtract LocalDate time
* /
public static void operatLocalDateDate() {
LocalDate today = LocalDate.now();
LocalDate previousYear = today.plusYears(-1).plusMonths(1).plusDays(1).plusWeeks(-1);
LocalDate previousYear1 = today.minusYears(1).plusMonths(1).plusDays(1).minusWeeks(1);
String result =previousYear.equals(previousYear1)?"Equal":"Unequal";
System.out.printf("PreviousYear1 time %s %s \n", previousYear, previousYear1,result);
}
Copy the code
Result: It can be seen that different operations have the same result, and it is necessary to think more about the time operation
The previousYear time 2020-02-20 is equal to the previousYear1 time 2020-02-20
Copy the code
Judge whether the date is early or late
Include isBefore() and isAfter comparison date comparisons in LocalDate
/ * *
* Determine whether the two dates are early or late
* /
public static void isBeforeOrAfter() {
LocalDate now = LocalDate.now();
LocalDate plusDays = now.plusDays(1);
String beforeSrt = now.isBefore(plusDays)?"Yes":"Not";
String afterSrt = plusDays.isAfter(now)?"Yes":"Not";
System.out.printf("Time %s %s before time %s \n",now, beforeSrt, plusDays);
System.out.printf("Time %s %s after time %s \n",plusDays, afterSrt, now);
}
Copy the code
The results of
The time 2021-01-26 is before the time 2021-01-27
The time 2021-01-27 is after the time 2021-01-26
Copy the code
Use Period to calculate the number of days and months between two dates
/ * *
* Calculate the number of days and months between dates
* /
public static void clacDateDays() {
LocalDate now = LocalDate.now();
LocalDate plusMonths = now.plusMonths(2).plusDays(-1);
Period between = Period.between(now, plusMonths);
System.out.printf("Difference between time %s and time %s %s month %s day \n",now, plusMonths,between.getMonths(), between.getDays());
}
Copy the code
The results of
Time 2021-01-26 and time 2021-03-25 difference of January 27 days
Copy the code
Get timestamp
/ * *
* Get the timestamp
* /
public static void getTimestamp() {
Instant now = Instant.now();
System.out.println(now);
}
Copy the code
Conclusion:
Date and Calendr classes are thread-safe and Calendr classes are not thread-safe. The Calendr and Date apis are thread-safe and Calendr classes are thread-safe.
If you like me, you can follow me. Thank you
Making address: https://github.com/bangbangzhou/greemes/tree/master