I was studying about java.time
package. Don't understand its need.
I mean previously there are time related classes in java.util
and
java.text
.
How java.time
differs from those?

- 1
- 1
-
2https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html – ByeBye Jun 13 '17 at 08:45
-
Is [this](https://stackoverflow.com/questions/24631909/differences-between-java-8-date-time-api-java-time-and-joda-time) your question? – Turtle Jun 13 '17 at 08:48
-
2You're about to start a fire ;) - The Java Time API is an effort to resolve the underlying drawbacks of both `Date` and `Calendar` and unify the APIs around a series of standards - If you've spent any time with the original APIs, you will find that the new APIs (while they seem daunting to begin with) are SO much easier to use and more feature rich – MadProgrammer Jun 13 '17 at 08:48
-
@Nathan No, that compares Joda-Time which was the inspiration for the Java 8 Time API (more or less) – MadProgrammer Jun 13 '17 at 08:48
-
2There's an explanation in [Jon Skeet's blog](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/) about `java.util.Date` and why it shouldn't be used. Note that `Calendar` is even worse. – RealSkeptic Jun 13 '17 at 08:49
-
@MadProgrammer The `java.time` API was designed by the same person who invented Joda Time (Stephen Colebourne) so it was definitely an inspiration. – Jesper Jun 13 '17 at 08:55
-
@Jesper Yeah, I know the original proposal came from him, but as I understand, it's been alter (and some features are still to be implemented) – MadProgrammer Jun 13 '17 at 09:00
-
Check [this](https://stackoverflow.com/questions/1571265/why-is-the-java-date-api-java-util-date-calendar-such-a-mess) and [this](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api) – Jun 13 '17 at 14:39
-
Also duplicates: [Why is the Java date API (java.util.Date, .Calendar) such a mess?](https://stackoverflow.com/q/1571265) – Basil Bourque Jun 14 '17 at 16:26
-
@RealSkeptic, thanks. the blog helped... – Akshay K Jun 15 '17 at 09:17
2 Answers
Oracle defined those changes and their reasons here:
Core ideas of this new API:
The new API is driven by three core ideas:
Immutable-value classes. One of the serious weaknesses of the existing formatters in Java is that they aren’t thread-safe. This puts the burden on developers to use them in a thread-safe manner and to think about concurrency problems in their day-to-day development of date-handling code. The new API avoids this issue by ensuring that all its core classes are immutable and represent well-defined values.
Domain-driven design. The new API models its domain very precisely with classes that represent different use cases for Date and Time closely. This differs from previous Java libraries that were quite poor in that regard. For example, java.util.Date represents an instant on the timeline—a wrapper around the number of milli-seconds since the UNIX epoch—but if you call toString(), the result suggests that it has a time zone, causing confusion among developers.
This emphasis on domain-driven design offers long-term benefits around clarity and understandability, but you might need to think through your application’s domain model of dates when porting from previous APIs to Java SE 8.
Separation of chronologies. The new API allows people to work with different calendaring systems in order to support the needs of users in some areas of the world, such as Japan or Thailand, that don’t necessarily follow ISO-8601. It does so without imposing additional burden on the majority of developers, who need to work only with the standard chronology.
In Java8 A new Date-Time API is introduced to cover the following drawbacks of old date-time API −
- 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.
- 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.
- 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.

- 7,643
- 6
- 34
- 62
-
1
-
Yes, I did and also gave reference to the same website because they have explained in very detailed manner. – Neeraj Jain Jun 13 '17 at 08:51
-
@NeerajJain You could'v elink to the [oracle blog](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html) too, it's better to get the real source. – Turtle Jun 13 '17 at 08:53