Introduction to New Date and time classes in Java 8

Source: Internet
Author: User

This article is mainly about the new date and time APIs in Java8. The new date and time classes were started by the Java developer Community awaited. The date class that existed before Java8 was always criticized, and many people would choose to use a third-party date library Joda-time. The date and time APIs in Java8 were developed by the authors of Jodatime, realizing the full contents of JSR310. These new APIs are all under Package java.time.

Since the joda-time,date4j of the third party are strong enough, why java8 to re-implement him, part of the reason is that these third-party libraries are compatible issues, such as the standard JSF date converter and Joda-time API, is incompatible, Each use of the need to write their own converter, so the standardized API is necessary, there is a jsr310,java8 in the implementation of his full set of content.

Design principles behind the new date class and the time class:

Immutable classes

Before Java8, the date class is a mutable class. When we use it in a multithreaded environment, programmers should confirm the thread safety of the date object. Java8 's date and time APIs provide a thread-safe immutable class. Programmers do not have to consider concurrency issues.

Domain Model Driven design approach

The new date and time category follows the domain-driven design. It is easy for developers to understand the functionality of methods and classes.

Now let's look at the new date and time APIs:

Java.time.LocalDate:

Localdate only available dates do not provide time information. It is immutable and thread-safe.

 Package Org.smarttechie;import Java.time.localdate;import java.time.temporal.chronounit;/*** This class  Demonstrates JAVA 8 data and time api* @author Siva Prasad Rao janapati* */public class Datetimedemonstration {/*** @param    Args*/public static void Main (string[] args) {//create date localdate localdate = Localdate.now ();    System.out.println ("The Local date is::" + localdate); Find the length of the month.   That's, how many days was there for this month.    System.out.println ("The number of days available for this month::" + localdate.lengthofmonth ());    Know the month name System.out.println ("What's The month name?::" + localdate.getmonth (). name ());   Add 2 days to the today's date.    System.out.println (Localdate.plus (2, chronounit.days));    Substract 2 days from Today System.out.println (Localdate.minus (2, chronounit.days));  Convert the string to date System.out.println (Localdate.parse ("2017-04-07")); }}

Java.time.LocalTime:

LocalTime only provides time and does not provide date information, it is immutable and thread-safe.

Package Org.smarttechie;import Java.time.localtime;import java.time.temporal.chronounit;/*** This class demonstrates  JAVA 8 data and time api* @author Siva Prasad Rao janapati* */public class Datetimedemonstration {/*** @param args*/public static void Main (string[] args) {   //get local time   localtime localtime = Localtime.now ();   System.out.println (localtime);  Get the hour of the day  System.out.println ("The Hour of the day::" + localtime.gethour ());  Add 2 hours to the time.  System.out.println (Localtime.plus (2, chronounit.hours));  Add 6 minutes to the time.  System.out.println (Localtime.plusminutes (6));  Substract 2 hours from the current time  System.out.println (Localtime.minus (2, chronounit.hours));}}

Java.time.LocalDateTime:

LocalDateTime provides time and date information that is immutable and thread-safe

Package Orr.smarttechie;import Java.time.localdatetime;import java.time.temporal.chronounit;/*** This class Demonstrates JAVA 8 data and time api* @author Siva Prasad Rao janapati**/public class Datetimedemonstration {/*** @param Args*/public static void Main (string[] args) {    //get LocalDateTime object    LocalDateTime LocalDateTime = Localdatetime.now ();    System.out.println (localDateTime);    Find the length of month. That's, how many days was there for this month.    System.out.println ("The number of days available for this month::" + localdatetime.getmonth (). Length (true));    Know the month name    System.out.println ("What's The month name?::" + localdatetime.getmonth (). name ());    Add 2 days to today ' s date.    System.out.println (Localdatetime.plus (2, chronounit.days));    Substract 2 days from today    System.out.println (Localdatetime.minus (2, Chronounit.days));}  }

Java.time.Year:

Year provides information for years, which is immutable and thread-safe.

Package Orr.smarttechie;import Java.time.year;import java.time.temporal.chronounit;/*** This class demonstrates Java 8 Data and Time api* @author Siva Prasad Rao janapati**/public class Datetimedemonstration {/*** @param args*/public static void Main (string[] args) {   //get year year   = Year.now ();   System.out.println ("Year::" + year);   Know the year is a leap year or not   System.out.println ("was year[" +year+ "] leap year?" + year.isleap ());}  }

Java.time.Duration:

Duration is used to calculate how many seconds between two given dates, how many milliseconds it is immutable, and thread-safe

Java.time.Period:

Period is used to calculate how many days, months, or years between two given dates, it is immutable and thread-safe

Package Orr.smarttechie;import Java.time.localdate;import Java.time.period;import java.time.temporal.chronounit;/*  * * This class demonstrates JAVA 8 data and time api* @author Siva Prasad Rao janapati**/public class Datetimedemonstration {/*** @param args*/public static void Main (string[] args) {   localdate localdate = Localdate.now ();   Period Period = Period.between (Localdate, Localdate.plus (2, chronounit.days));   System.out.println (Period.getdays ());}  }

Link: http://www.codeceo.com/article/java-8-date-time-guide.html
English Original: Java 8 New Date and Time overview
Translation Code Rural Network – Chestnut sorghum
[ reproduced in the text must be marked and retained in the original link, translation links and translators and other information. ]

Introduction to New Date and time classes in Java 8

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.