New date and time in Java8 _java

Source: Internet
Author: User
Tags local time month name time and date

The design principles behind the new date class and the time class:

Non-variable class

The date class is a mutable class before java8. When we use it in a multithreaded environment, programmers should confirm the thread safety of the date object. The date and time APIs for JAVA8 provide thread-safe immutable classes. Programmers do not have to consider concurrent issues.

Domain Model Driven Design method

The new date and time categories follow the domain-driven design. It is easy for developers to understand the capabilities of methods and classes.

Next, let's take a look at the new date and time APIs:

1.java.time.localdate:

Localdate only provide dates without providing time information. It is immutable and thread-safe.

 package org.smarttechie; import java.time.LocalDate; import java.time.temporal.ChronoUnit; /** * This class demonstrates the JAVA 8 data and Time API * @author Siva Prasad Rao Janapati * */public class Datetimedemons tration {/** * @param args/public static void main (string[] args) {//create date localdate localdate = localdate.no
  W (); 
  System.out.println ("The Local date is::" + localdate); Find the length of the month.
  That's, how many days are 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 is the month name?::" + localdate.getmonth (). name ());
  Add 2 days to the "Today" 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")); }
}

2.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 the JAVA 8 data and Time API
* @author Siva Prasad Rao Janapati * */public
Clas s 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": "+ 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 current time
 System.out.println (Localtime.minus (2, chronounit.hours));
 }

3.java.time.localdatetime:

The

LocalDateTime provides time and date information, which is immutable and thread-safe

 package orr.smarttechie; import java.time.LocalDateTime; import
Java.time.temporal.ChronoUnit; /** * This class demonstrates the JAVA 8 data and Time API * @author Siva Prasad Rao Janapati * */public class Datetimedemons tration {/** * @param args/public static void main (string[] args) {//get LocalDateTime object LocalDateTime Local
  DateTime = Localdatetime.now ();
  System.out.println (LocalDateTime); Find the length of month.
  That's, how many days are 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 is 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)); }
}

4.java.time.year:

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

Package Orr.smarttechie;
Import Java.time.Year;
Import Java.time.temporal.ChronoUnit;
/**
* This class demonstrates the JAVA 8 data and Time API
* @author Siva Prasad Rao
janapati
*
*/Public Class Datetimedemonstration {
/**
* @param args */public
static void Main (string[] args) {
  // Get year of year
  = Year.now ();
  System.out.println ("Year::" + year);
  Know the year are leap or
  System.out.println ("is year[" +year+) leap year? + year.isleap ());
 }

5.java.time.duration:

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

6.java.time.period:

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

 package orr.smarttechie; import java.time.LocalDate; import java.time.Period; Import ja
Va.time.temporal.ChronoUnit; /** * This class demonstrates the JAVA 8 data and Time API * @author Siva Prasad Rao Janapati * */public class Datetimedemons
  tration {/** * @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 ()); }
}

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.