Java 8-Java CoreNo Comments

With Java 8, a new Date-Time API is introduced to cover the following drawbacks of old date-time API −

  1. Not thread safe − java.util.Date is not thread safe, thus developers have to deal with concurrency issue while using date. The new date-time API is immutable and does not have setter methods.
  2. Poor design − Default Date starts from 1900, month starts from 1, and day starts from 0, so no uniformity. The old API had less direct methods for date operations. The new API provides numerous utility methods for such operations.
  3. Difficult time zone handling − Developers had to write a lot of code to deal with timezone issues. The new API has been developed keeping domain-specific design in mind.

Java 8 introduces a new date-time API under the package java.time. Following are some of the important classes introduced in java.time package −

  1. Local − Simplified date-time API with no complexity of timezone handling.
  2. Zoned − Specialized date-time API to deal with various timezones.

Local Data-Time API

LocalDate/LocalTime and LocalDateTime classes simplify the development where timezones are not required. Let’s see them in action −

Java8Tester.java

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Tester as follows −

It should produce the following output −

Zoned Date-Time API

Zoned date-time API is to be used when time zone is to be considered. Let us see them in action −

Java8Tester.java

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Tester as follows −

It should produce the following output −

Chrono Units Enum

java.time.temporal.ChronoUnit enum is added in Java 8 to replace the integer values used in old API to represent day, month, etc. Let us see them in action −

Java8Tester.java

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Tester as follows −

It should produce the following result −

Period & Duration

With Java 8, two specialized classes are introduced to deal with the time differences −

  1. Period − It deals with date based amount of time.
  2. Duration − It deals with time based amount of time.

Let us understand them with an example −

Java8Tester.java

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Tester as follows −

It should produce the following output −

Temporal Adjusters

TemporalAdjuster is used to perform the date mathematics. For example, get the “Second Saturday of the Month” or “Next Tuesday”. Let us see an example of it −

Java8Tester.java

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Tester as follows −

It should produce the following result −

Backward Compatibility

A toInstant() method is added to the original Date and Calendar objects, which can be used to convert them to the new Date-Time API. Use an ofInstant(Insant,ZoneId) method to get a LocalDateTime or ZonedDateTime object. Let us understand it with an example −

Java8Tester.java

Verify the Result

Compile the class using javac compiler as follows −

Now run the Java8Tester as follows −

It should produce the following output −


Be the first to post a comment.

Add a comment


Warning: stream_socket_client(): unable to connect to http://browscap.org/version (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 1366

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 1004