Using JAVA8 to easily process dates

Source: Internet
Author: User

Oracle may also be aware of the lack of Java processing dates, so new localdate and localtime interfaces are added to JAVA8.

It is well known that the month of Java.util.Date is from the beginning of 0 ~0 representative January, 1 represents February ~ hehe.

Indeed when the beginning of contact with the author also used very tangled ~ ~ very irritable ~ but Java8 's localdate has improved this ~ ~ Because the localdate month is stored using enumerations (enum).

Mother no longer need to worry I use wrong ~ ~ ~ ~

It is believed that some readers have found that ~java.util.date can store the date of the month or even milliseconds ~ ~ and java8 divided into two parts localdate and localtime. That is, Localdate is used to store the processing date and time, and the localtime is used to store the processing in milliseconds. ~ Division of Labor Clear ~ so if you want to store the date and time. There's a localdatetime. basic usage Get the current time is similar to Java.util.Date's usage: Java.util.Date is:

Date Date=new date ();
As soon as new comes out there is information about the current date, and localdate is used in this way:
Localdate Ld=localdate.now ();
Is it semantically more understandable? A now represents the current time.

It can be written according to the date of year:
Localdate Ld2=localdate.of (2015,12,30);
Note that 12 of the above month represents December instead of "1 March" ~ haha

Get by string:
Localdate ld3=localdate.parse ("2015-08-20");
According to the string to get localdate format there are certain requirements: to be in the form of yyyy-mm-dd, such as February can not write 2 must be written 02 to
If you take the first day of this month you can write this:
Localdate Firstday=ld3.with (Temporaladjuster.firstdayofmonth ());
Take the last day of this month to do the same:
Localdate Lastdat=ld3.with (Temporaladjusters.lastdayofmonth ());

Take the nth day of this month:
Localdate day=ld3.withdayofmonth (n);
Take the next day of the current date:
Localdate nextday=lastday.piusdays (1);
Take the first Monday of August 15:



here is the use of localtimeThe use of localtime is almost the same as the use of Locald, for example, to obtain the current time:
LocalTime Now=localtime.now ();
This will allow you to get the current time in seconds
The construction time is divided into two methods, the first of which is to use a certain format string:
LocalTime  time=localtime.parse ("09:52:00");
The second type is:
LocalTime zero = localtime.of (9, 52, 0);


Finally say ~ ~ Since the date can be constructed so naturally the value of the date ~ ~ To obtain the method is also very convenient ~ ~ no longer write directly on a picture of their own understanding it ~






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.