Java 8 – Date Time API

Introduction

Java 8 comes with a much improved and much required change in the way date and time is handled. Almost all of us have experienced the pains of working with date in java. Most of us have switched to Joda Time, but java 8 has changed that with a much cleaner and more extensive API. Before we look at the API lets look at the date and time concepts. The java date follows the Gregorian Calendar rules. The classes that represent the date and time concepts are present in the java.time package. The important classes in this package are:

  • 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 ’29 seconds’
  • java.time.Instant: This represents a particular instant in the timeline. It stores the number of seconds through epoch time and also has 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 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 be used to convert to local time
  • java.time.ZoneOffset:This stores the time zone offset from UTC. The zone rules are stored in ZoneId.
  • java.time.OffsetDateTime:This stores the local datetime with the offset. This class does not have information about Zone Rules.

Lets look at examples for these, later in this tutorial we will look at ways to format and parse time.


Creating a local date

This creates a new time instant. The instant does not have timezone info. If you print the instant it prints the time with UTC time zone.


Epoch seconds

The epoch seconds is the number of seconds since 1970-01-01T00:00:00Z


Adding time to an Instant

the plus function allows adding time intervals. The time intervals can be NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS


Subtracting time from an Instant

The minus function allows subtracting time from an instant. It allows the same time intervals as plus.


Comparing two instants

The compare function can be used to compare two dates. It returns -1 if the date that is passed is after , 1 if it is before


check if one instant is after another


create a LocalDateTime

Note that this gets the time in the local time zone


Convert LocalDateTime to datetime in particular zone

This creates and instance of ZonedDateTime


Get the day of week from DateTime


Get the day of year from DateTime

The other fields that can be returned are 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, YEAR, OFFSET_SECONDS (offset from UTC).


Get the LocalDate part of LocalDateTime


Get the LocalTime part of LocalDateTime


Create a LocalDateTime from year, month, day, hour, min


Create LocalDateTime by parsing a string


Create LocalDateTime in different timezone


Create LocalDateTime from an Instant and a timezone


Create a ZonedDateTime


Find the difference between two times in specific units


Get the offset for the current zonedDateTime


Parsing or formatting a datetime using DateTimeFormatter


Converting ZoneDateTime to another zone.
There are two ways to do this. In the first method, the code maintains the same instant but changes the timezone. In the second method the code just changes the timezone but the localtime is constant. Look at this example.


The above examples give some of the method present in the DateTime API. There are many other methods but this should suffice for basic usage. If you have a specific problem then do drop us a mail or put in your comments and we will be happy to answer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top
Bitnami