Monday, 10 March 2014

Why do we have new java.time package introduced in java 8 ?


Why do we have new java.time package introduced in java 8 ?

Java 8 : The new Date and Time API was developed to overcome numerous problems with Java's previous date and time APIs.

 As a result, it has been architected around a number of important design principles:

Immutability and thread safety: All of the Date and Time API's core classes are immutable, which ensures that you don't have to worry about threading issues caused by lack of thread synchronization. Immutable objects are simple to construct, use, and test, they make for good hash keys, etc.

Fluency: Date and Time presents a fluent interface, which should make its methods more readable and easier to learn, especially when chained together. Fluent factory methods (e.g., now(), from(), and of-prefixed methods) are used as an alternative to constructors. You'll also be able to use with-prefixed methods if you need to return a copy of the current instance with additional information.

Clarity: Each method in the Date and Time API is well-defined and clear about what it accomplishes. Additionally, Date and Time rejects null arguments early. Unless otherwise noted, passing a null argument to a method in any class or interface will cause a NullPointerException to be thrown. Validation methods that take object arguments and return Boolean values are an exception: they generally return false when null is passed.

Extensibility: The Strategy design pattern is used throughout the API to allow for extension while avoiding confusion. For example,  you could even introduce your own calendar.


No comments:

Post a Comment