Java uses LocalDate to calculate the age based on the date. javalocaldate

Source: Internet
Author: User
Tags date1

Java uses LocalDate to calculate the age based on the date. javalocaldate

There are many classes that are directly related to dates in Java. Commonly Used classes are Date and Calendar under java. util package. java. text. SimpleDateFormat is also used when the format is used.

First of all, it is awkward to use Date and Calendar. I am not familiar with it at the beginning. I cannot tell the specific usage and difference of Date and Calendar classes.

In addition, the set Method of the Calendar class has some anti-human nature when setting the date. When setting the month, you always need to make up your mind to subtract 1. For example, to set it to January 6, 2018, you need to set it to, 0, 6, in this example, January is represented by 0:

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

For specific reasons, see the answer here: StackOverflow: Why is January month 0 in Java Calendar?

I don't know much about it ..

A simple requirement for a few days ago is to calculate the age based on two dates. I checked it online and directly abandoned the difficult Date and Calendar classes. I used java. time. LocalDate directly. Let's talk about the 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 result:

You're 27 years old.

It's nice to discard the inter-conversion call between the slightly complicated and big Date and Calendar classes, and the code reading is also very clear!

Java. time is a new date and time library introduced in Java 8. The following methods are 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, LocalDate is an immutable class (immutable class), so like the String class, it can be used without a new object (?)

Then the LocalDate class is used to set the date of the method:

public static LocalDate of(int year, Month month, int dayOfMonth)public static LocalDate of(int year, int month, int dayOfMonth)

Of has a total of three overload methods. Here are two of them. The first Month class is an enumeration type that contains the English names of the Month, such as JANUARY and NOVEMBER.

The second one is to set the year, month, and day normally. The good news is that the month starts from 1, which means you no longer need to manually subtract 1 !!!

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 the Period type.

Period represents a Period of time in the form of "2 years, 3 months, 4 days. The getYears method called next belongs to the Period class.

Finally, let's take a look at some methods to obtain the time interval of 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. Yes, it's that simple.

Summary

The above section describes how to use LocalDate in Java to calculate age by date. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in time!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.