Java uses LocalDate to calculate age by date

  • 2020-12-16 05:57:51
  • OfStack

There are many classes in Java that are directly related to date. The most commonly used ones are java.util package and Date, and java.text.SimpleDateFormat

First of all, Date and Calendar are awkward to use in practice. At the beginning, I was not familiar with them, and I always couldn't tell the specific usage and difference between Date and Calendar.

In addition, the set method of Calendar class has some anti-human methods when setting the date. When setting the month, you should always think of 1 minus 1. For example, to set the date to January 6, 2018, you should set it to 2018,0 and 6, because January is represented by 0 here:


Calendar cal = Calendar.getInstance();
cal.set(2018, 0, 8);

The specific reason can be seen here: StackOverflow: Why January month 0 in Java Calendar?

I can't stand it anyway.

The other day there was a simple requirement to calculate the age based on two dates. After checking 1 on the Internet, Date and Calendar classes, which are difficult to use, are discarded, and java.time.LocalDate is used to do it. Without further ado, code directly:


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 results:


You're 27 years old.

It's nice to get rid of the slightly more complex and heady calls between the Date and Calendar classes, and the code reads particularly well.

java.time is a new date and time library introduced in Java 8. The following methods are included in the java.time package.

Here's how this code calculates the time difference in a simple and refreshing way:

First, LocalDate is 1 immutable class (immutable class), so like String class 1, you don't need new1 new objects to use (?).

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)

of1 has three overloaded methods, two of which are listed here. The first Month class is an enumeration containing the English names of the months, such as JANUARY, NOVEMBER, and so on

The second is a perfectly normal setting of year, month and day. The good news is that month starts at 1, which means you don't have to manually subtract 1 anymore!!

Here are the until methods used by the LocalDate class to compare dates:


public Period until(ChronoLocalDate endDateExclusive)

This method returns an object of type Period.

Period represents a time interval in the form of "two years, three months, four days". The getYears method that is called next belongs to the Period class.

Finally, take a look at the Period class 1 for some methods of getting time intervals:


public int getYears()
public int getMonths()
public int getDays()

These three methods are used to get the year, month, and day of the interval, respectively. Yes, it's that simple.

conclusion

The above is the Java site to you in the use of LocalDate according to the date to calculate the age of the implementation method, I hope to help you, if you have any questions welcome to give me a message, this site will reply you in time!


Related articles: