Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Date & Time API in Java 8

On this page i read the following:

To do calculations with dates, it is also very easy. Probably the best improvement compared to the current situation with Java < 1.8:

Period p = Period.of(2, HOURS);
LocalTime time = LocalTime.now();
LocalTime newTime = time.plus(p); // or time.plus(5, HOURS); or time.plusHours(5); 

I don't clearly see the advantage prior to Versions < 1.8.

Maybe someone can give me an example? Atm i am asking myself, where the improvement of the new date & time API comes from.

like image 365
Chris311 Avatar asked Jul 08 '13 13:07

Chris311


People also ask

What is new Date ()?

Use new Date() to get a Date for the current time or Date. now() to get the current time in milliseconds since 01 January, 1970 UTC. Spec. Date() : String. Returns a string representation of the current date and time.

Is new Date () a string?

Calling the Date() function (without the new keyword) returns a string representation of the current date and time, exactly as new Date().

What is the difference between Date and new Date?

new Date creates a new Date object that you can modify or initialize with a different date while Date returns a string of the current date/time, ignoring its arguments.

How do you create a Date?

You can create a Date object using the Date() constructor of java. util. Date constructor as shown in the following example. The object created using this constructor represents the current time.


1 Answers

With Java < 8, you would need to write something like:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + 2);

vs. with Java 8:

LocalTime now = LocalTime.now();
LocalTime later = now.plus(2, HOURS);

The improvements are essentially on

  • readability:
    • Calendar.getInstance() is not very well named: it is hard to tell which instance you are getting without reading the Javadoc. LocalTime.now() is quite self-describing: you get a time and it is now.
    • To offset a date, you call an offsetting method (plus) whereas with the Calendar API, you have to manually change the fields of the object (in this example, the hour) which is error prone.
  • ease of use (see for example the table towards the bottom of this page for a comparison):
    • the Calendar API is complicated to use because it mixes concepts, such as a simple date (June 26th 2015) and an instant in time (June 26th 2015 at 10am UTC) - there isn't a class for the former concept
    • The new Time API has a clear separation between the various date/time concepts
  • safety:
    • The Calendar API is not safe: nothing prevents you from writing cal.set(123, 2) which would throw a not-so-helpful ArrayOutOfBoundsException. The new API uses enums which solves that problem.
    • The new API uses immutable objects, which makes it thread safe.

Overall, the new API is significantly inspired from jodatime which has been the preferred Java Date API for quite some time now. You can also read this detailed comparison of Java (<1.8) date vs. JodaTime (most of it should apply to the Java 8 Date API).

like image 118
assylias Avatar answered Sep 24 '22 01:09

assylias