Java Date Time API (Java 8 new feature)

Source: Internet
Author: User
Tags epoch seconds epoch time local time

Java 8–date Time API

Java 8 comes with a much improved and much required change in the the-the-time and time is handled.
The classes that represent the date and time concepts is present in the Java.time package.

The important classes in this package is:
  • Java.time.Period:This class represents the date part of the DateTime. It represents the date part in terms of days, months and years. For example 1 year, 2 months and 5 days
  • Java.time.Duration:This class represents the time part of the DateTime. It represents the time part in terms of seconds and nanoseconds. For example ' seconds '
  • Java.time.Instant:This represents a particular Instant in the timeline. It stores the number of seconds through epoch time and also have another field that stores the nanoseconds
  • Java.time.LocalDate:This stores the date part of date-time in terms of years-months-days. It does not store the TimeZone. The class is immutable.
  • Java.time.LocalTime:This class stores the time part of a date time without any TimeZone info.
  • Java.time.LocalDateTime:This class stores the localdate and localtime but no TimeZone
  • Java.time.ZonedDateTime:This class stores the LocalDateTime and the TimeZone info as a Zoneoffset object. The class has access to Zonerules which can is used to convert to local time
  • Java.time.ZoneOffset:This stores the time zone offset from UTC. The zone rules is stored in ZoneID.
  • Java.time.OffsetDateTime:This stores the local datetime with the offset. This class does not has information about Zone Rules.
1  PackageJava8.datetime;2 3 Importjava.time.LocalDate;4 ImportJava.time.LocalTime;5 6 /**7 * Java 8–date time API8 * LocalTime: Current time9 * Localdate: Current dateTen  */ One  Public classLocalTimeDemo3 { A      Public Static voidMain (string[] args) { -LocalTime localtime (); -System.out.println ("Current time:" +localtime); the  -Localdate localdate (); -System.out.println ("Current date:" +localdate); -     } +}

1  PackageJava8.datetime;2 3 ImportJava.time.DayOfWeek;4 ImportJava.time.LocalDateTime;5 Importjava.time.ZoneId;6 ImportJava.time.temporal.ChronoField;7 8 /**9 * Java 8–date time APITen * LocalDateTime One * URL: A  */ -  Public classLocalDateTimeDemo2 { -      Public Static voidMain (string[] args) { the //Create a LocalDateTime -LocalDateTime LocalDateTime (); -System.out.println ("New LocalDateTime object:" + localDateTime);//Prints 2018-02-12t15:08:44.116 -  + //Convert LocalDateTime to datetime in particular zone -System.out.println ("Specify Time zone:" + Localdatetime.atzone (zoneid.of ("America/new_york"))); +  A //Get the day of week from DateTime atSystem.out.println ("Day of the Week:" + dayofweek.from (localDateTime));//prints SUNDAY. -  - //Get the day's from DateTime -System.out.println ("Day of the Year:" + Localdatetime.get (chronofield.day_of_year));//Prints - //The other fields and can be returned is Minute_of_hour, Minute_of_day, HOUR_OF_AMPM, - //Hour_of_day, Ampm_of_day, Day_of_week, Day_of_month, Day_of_year, Month_of_year , in //Year , Offset_seconds (OFFSET from UTC). -  to //Get Current date +System.out.println ("Current date:" +localdatetime.tolocaldate ()); -         //Prints 2014-09-29 the  *         //Get current Time $System.out.println ("Current time:" +localdatetime.tolocaltime ());Panax Notoginseng         //Prints 22:26:30.146 -  the //Create a LocalDateTime from year, month, day, hour, Min +System.out.println ("Instantiating LocalDateTime by Int:" + localdatetime.of (2014, 10, 1, 10, 0)); A         //Prints 2014-10-01t10:00 the  + //Create LocalDateTime by parsing a string -LocalDateTime parsedlocaldatetime = Localdatetime.parse ("2014-01-01t11:00"); $System.out.println ("Instantiating LocalDateTime by String:" +parsedlocaldatetime); $  -System.out.println ("Current hour point:" +Localdatetime.gethour ()); -  the  -     }Wuyi}


1  PackageJava8.datetime;2 3 Importjava.time.Instant;4 ImportJava.time.temporal.ChronoUnit;5 6 /**7 * Java 8–date time API8 * Instant9 * URL:  */ One  Public classInstantDemo1 { A      Public Static voidMain (string[] args) { -         //Creating a local date -Instant now (); the         //2018-02-12t06:29:01.493z -System.out.println ("Current time:" +Now ); -  - //The epoch seconds is the number of seconds since 1970-01-01t00:00:00z +System.out.println ("Current number of seconds:" +Now.getepochsecond ()); -  + //Adjust Time A //The plus function allows adding time intervals. at //The time intervals can NANOS, MICROS, Millis, SECONDS, MINUTES, HOURS, Half_days , Days -Instant tomorrow = (2, chronounit.days); -System.out.println ("Tomorrow Time:" +tomorrow); - //The minus function allows subtracting time from an instant. -Instant yesterday = Now.minus (1, chronounit.days); -System.out.println ("Yesterday Time:" +yesterday); in  - //The compare function can be used to compare and the dates. to //it returns-1 If the date is passed are after, 1 if it is before +System.out.println ("Today and Tomorrow Compare:" + Now.compareto (Tomorrow));//prints-1 -  the //Check if one instant is after another *System.out.println ("Whether today is behind yesterday:" + now.isafter (yesterday));//Prints True $ Panax Notoginseng     } -}

Java Date Time API (Java 8 new feature)

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: 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.