There are many classes directly related to date in Java. The most commonly used ones are Date and Calendar under java.util package. When you need to use the format, you will also use java.text.SimpleDateFormat.
First of all, let me complain that Date and Calendar are actually a bit awkward to use. I was not familiar with it when I first used it, and I always couldn't tell the specific usage and differences between Date and Calendar classes.
Moreover, the Calendar class set method has some anti-humanity when setting dates. When setting month, you always have to make up for it and subtract 1. For example, if you want to set it to January 6, 2018, you have to set it to 2018, 0, 6, because January is represented by 0 here:
Calendar cal = Calendar.getInstance();cal.set(2018, 0, 8);
For the specific reasons, please refer to the answer here: StackOverflow: Why is January month 0 in Java Calendar?
Anyway, I can't stand it. .
A few days ago, there was a simple requirement, which was to calculate age based on two dates. I checked the Internet and directly abandoned the difficult Date and Calendar classes and directly used java.time.LocalDate. Without further ado, just upload the code:
import java.time.LocalDate;public class TestLocalDate { public static void main(String[] args) { LocalDate date1 = LocalDate.of(2018, 1, 6); LocalDate date2 = LocalDate.of(1991, 1, 3); int age = date2.until(date1).getYears(); System.out.println("You're " + age + " years old."); }}Output result:
You're 27 years old.
The slightly complex and overwhelming mutual conversion calls between the Date and Calendar classes are abandoned, and the code is also very clear. Isn’t it very pleasant?
java.time is a date and time library newly introduced in Java 8. The following methods are all in the java.time package.
Let’s take a look at how this code calculates the time difference in a simple and refreshing way:
First of all, LocalDate is an immutable class (immutable class), so like String class, you don't need to new a new object to be used (?)
Then there is the of method used by the LocalDate class to set the date:
public static LocalDate of(int year, Month month, int dayOfMonth)public static LocalDate of(int year, int month, int dayOfMonth)
There are three overloaded methods in total, two of which are listed here. The first Month class is an enum type that contains the English name of the month, such as JANUARY, NOVEMBER, etc.
The second is the normal setting of the year, month and day. The good news is that the month here starts from 1, which means you no longer need to subtract 1 manually! ! !
The following is the until method used by the LocalDate class to compare dates:
public Period until(ChronoLocalDate endDateExclusive)
This method returns an object of type Period.
Period represents a period of time intervals that are shaped like "2 years, 3 months, 4 days". The next getYears method called belongs to the Period class.
Finally, let’s take a look at some methods for obtaining time intervals in the Period class:
public int getYears()public int getMonths()public int getDays()
These three methods are used to obtain the year, month and day of the time interval respectively. Yes, it's that simple.
Summarize
The above is the implementation method of using LocalDate to calculate age based on dates in Java introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time!