DateTime API in Java 8 has overcome many shortcomings of older java.util.Date and java.util.Calendar. As there were many issues with the older Date and Calendar API. So now let us see how this java 8 DateTime API resolve the issues and how it provides the better support in JDK core.

Issues in Existing Date/Time APIs :

  1. Thread Safety.
  2. Zoned Date and Time.
  3. Design and Easiness.

Core Idea of Java 8 DateTime API :

The above-mentioned issues are resolved by the Java 8 DateTime API. Following three points will explain how Java 8 DateTime API resolved the issues of Existing java formatter.

  • Immutable-value classes: The main issue in the existing java formatter is that they are not thread-safe. Java 8 DateTime API resolve this issues by ensuring that its core classes are immutable and represent the well-defined values.
  • Domain-Driven Design: In this new API, classes are more specific, that represent different use cases for Date and Time closely. Basically, new API focussed on Domain-Driven Design and offers long-term benefits around clarity and understandability.
  • Chronologies Issues: New API allows developers to use different zone calendering without any burden. There is no need to put any extra effort into doing so.

Local Date :

It represents a date in ISO format without time. It can be used to store dates such as birthdays or any specific occasions. Simply an instance of current date can be created by:

Image1

A local date with some specific day, month and year can be generated using of a method in the following way:

Image2

Local Time :

It represents a time without Date. Similarly to above-explained LocalDate, LocalTime can also be instantiated as:

Image3

Local Date/Time provide many methods that make the developer easy to code and optimize code in terms of operation related to Date and Time. Most commonly used methods are as follows:

  1. now()
  2. of()
  3. parse()
  4. plusDays()
  5. minus()
  6. getDayOfWeek()
  7. getDayOfMonth()
  8. isLeapYear()
  9. isAfter()
  10. isBefore()
  11. with()
  12. atStartOfDay()
  13. getHour()
  14. getMonth() and many more.

So the below code will tell us how we can use the methods of Date/Time API.

Image4

Image5

Zoned DateTime API :

In Java 8 we have a very good feature that if we have to deal with time zone specific date and time, zoneId is an identifier which is available in java.time.zoneId and can be used to represent any zone in the world.

Image6

Reference :

https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

 

Pasted image at 2017_11_27 04_17 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s