Processing methods for date and time in Java 8 _java

Source: Internet
Author: User
Tags new set

Java 8 new Localdate and LocalTime interfaces, why do you want to create a new set of processing date and time API? Because the old java.util.Date is too difficult to use.

Java.util.Date month starts from 0, January is 0, December is 11, pervert! Java.time.LocalDate month and week are changed to enum, it is impossible to use the wrong.

Both Java.util.Date and simpledateformatter are not thread-safe, and localdate and localtime, like the most basic string, are immutable types, not only thread-safe, but not modifiable.

Java.util.Date is a "universal interface" that contains dates, times, and milliseconds, and if you only want to use Java.util.Date to store dates, or to store only time, then only you know which parts of the data are useful and which parts of the data are unavailable. In the new Java 8, dates and times are clearly divided into localdate and localtime,localdate cannot contain time, and localtime cannot contain dates. Of course, LocalDateTime can contain both dates and times.

The reason the new interface is better used is that it takes into account the operation of datetime, which often occurs forward or backward for several days. With Java.util.Date calendar to write a lot of code, and the general developers are not necessarily able to write right.

Localdate

See how the new localdate is used:

Take the current date:
localdate today = Localdate.now ()///-> 2014-12-24///
by date of month, December is:
localdate Crischristmas = Localdate.of (2014, 12, 25); -> 2014-12-25
//According to string:
localdate Endoffeb = Localdate.parse ("2014-02-28"); 02 is not a 2, and of course there is an overloaded method that allows you to define a format

Localdate.parse ("2014-02-29"); Invalid date could not be passed: 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 this month, no more calculation is 28, 29,30 or:
localdate lastdayofthismonth = Today.with (Temporaladjusters.lastdayofmonth ());//2014-12-31
//
take off one day: localdate firstDayOf2015 = lastdayofthismonth.plusdays (1);//became 2015-01-01
//Taking the first Monday of January 2015, This calculation uses calendar to kill a lot of brain cells:
localdate firstMondayOf2015 = Localdate.parse ("2015-01-01"). With ( Temporaladjusters.firstinmonth (Dayofweek.monday)); 2015-01-05

LocalTime

LocalTime only contains time, how can you use Java.util.Date to only indicate time before? The answer is, pretend to ignore the date.

LocalTime contains milliseconds:

LocalTime now = Localtime.now (); 11:09:09.240

You may want to clear the number of milliseconds:

LocalTime now = Localtime.now (). Withnano (0)); 11:09:09

The construction time is also simple:

LocalTime zero = localtime.of (0, 0, 0); 00:00:00
LocalTime mid = Localtime.parse ("12:00:00"); 12:00:00

Time is also recognized in ISO format, but the following 3 formats can be identified:

12:00
12:01:02
12:01:02.345

Jdbc

The latest JDBC Mapping associates the date type of the database with the new type of Java 8:

SQL-> Java
--------------------------

Date-> Localdate
Time-> localtime
Timestamp-> LocalDateTime

There is no longer a situation that maps to java.util.Date where some parts of the date or time are 0.

The above is a small set to introduce the Java 8 in the date and time of the processing method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.