How to handle dates and times happily in Java 8

Source: Internet
Author: User
Tags new set

Java 8 new LocalDate and LocalTime interface, why do you have a new set of processing date and time API? Because the old java.util.Date really is too difficult to use.

java.util.DateMonth from 0 start, January yes 0 , December Yes 11 , pervert bar! The java.time.LocalDate month and the week are changed enum , it is impossible to use the wrong again.

java.util.DateAnd SimpleDateFormatter are not thread-safe, and LocalDate LocalTime as with the most basic String , are immutable types, not only thread-safe, but cannot be modified.

java.util.Dateis a "universal interface", it contains the date, time, and the number of milliseconds, if you only want to java.util.Date store the date, or only to store time, then only you know which parts of the data is useful, which part of the data is not available. In the new Java 8, dates and times are clearly divided into LocalDate and LocalTime , LocalDate cannot contain time, LocalTime cannot contain dates. Of course, LocalDateTime you can include both date and time.

The reason the new interface is better is that it takes a date-time operation and often pushes forward or pushes backwards for a few days. With java.util.Date mates Calendar to write a lot of code, and the general developers are not necessarily able to write right.

Localdate

Look at the new LocalDate how to use:

//take the current date:Localdate today = Localdate.now ();//2014-12-24//according to the date of the month, December is:Localdate Crischristmas = Localdate.of ( the, A, -);//2014-12-25
localdate lastdateofyear = Crischristmas.with (temporaladjusters.lastdateofyear); //according to the string fetch:Localdate Endoffeb = Localdate.parse ("2014-02-28");//in strict accordance with ISO yyyy-mm-dd verification, 02 written 2 No, of course, there is an overloaded method allows you to define the formatLocaldate.parse ("2014-02-29");//Invalid date failed: datetimeparseexception:invalid date

Date conversions are often encountered, such as:

//take the 1th day of the month:Localdate firstdayofthismonth = Today.with (Temporaladjusters.firstdayofmonth ());//2014-12-01//take the 2nd day of the month:Localdate Seconddayofthismonth = Today.withdayofmonth (2);//2014-12-02//take the last day of the month and no longer calculate whether it is 28,29,30 or:Localdate lastdayofthismonth = Today.with (Temporaladjusters.lastdayofmonth ());//2014-12-31//Remove the day:Localdate firstDayOf2015 = Lastdayofthismonth.plusdays (1);//into a 2015-01-01.//taking the first Monday of January 2015, this calculation uses calendar to kill many brain cells:Localdate firstMondayOf2015 = Localdate.parse ("2015-01-01"). With (Temporaladjusters.firstinmonth (Dayofweek.monday));//2015-01-05

LocalTime
localtime only contains time, how can we use java.util.Date to represent time? The answer is to pretend to ignore the date. LocalTime contains milliseconds: localtime now= Localtime.now ();//11:09:09.240you may want to clear the number of milliseconds: LocalTime now= Localtime.now (). Withnano (0));//11:09:09construction Time is also simple: localtime zero= Localtime.of (0,0,0);//00:00:00LocalTime mid = Localtime.parse ("12:00:00");//12:00:00time is also recognized in ISO format, but the following 3 formats are recognized: A:xx     A: on: Geneva     A: on:02.345

Jdbc
, Java--------------------------,  LocalDateTime will no longer appear to map to Java.util.Date where some part of the date or time is 0. 

Reference: http://www.liaoxuefeng.com/article/00141939241051502ada88137694b62bfe844cd79e12c32000

How to handle dates and times happily in Java 8

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.