Posts Tagged: date

And I fell in love with the date…Java 8 Date

Love is in the air year after year in the period when winter and spring fight for power.

In the week that Java8 turns one I am going to speak about the date I fell in love with…Java 8 Date.

Even though I have never considered the previous implementation of Dates as being a pain (just a little bit verbose :p) I am really appreciative and using it made me fall instanlty in love with the new Date.

What do I really like about Date? You may find listed below some(not all) reasons:

  • The possibility to use classes specify/use exactly what you need:

Why should I be forced to store the specific date (usually 1st January 1970) when I just need the hours?  was a question that arose probably in many situations. [I would really want to know how many GB of data occupies the 1st January 1970]

Java 8 resolved the issue with adding specific classes LocalDateTime, LocalDate and LocalTime classes for the “un-timezoned” feature and for each of these provides the zoned class, which stores also the timezone for each of them.

Also, the precision one needs to use can be specified

  • The easiness to get a reference to a new instance of a Date.

Seems like a dream not to use a Calendar class to get a new instance of a Date class:

LocalDateTime thisMoment = LocalDateTime.now();

LocalDate java8LauchDate = LocalDate.of(18, Month.MARCH, 2014)

LocalDate forTheNostalgics = LocalDate.ofEpochDay(1);

  • The beauty of the Period and Duration

Until this moment we specified precise moment in time, but sometimes we need to use an interval rather than specific points in time.

Period oneYearTwoMonthsThreeDays = Period.of(1,2,3);

and Period’s little brother (used for intervals within a day):

Duration snoozeDuration = Duration.ofMinutes(10);

  • One can do maths![How can one not love that]

The addition/substraction of specific periods is just a bliss:

LocalDateTime now = LocalDateTime.now().plusWeeks(1).plusDays(3).minusDays(7);

  •  The usage of the “normal” number for the month.

Just imagine in a previous Java version my birthday would have been declared like. Ok, I am  a developer and used with 0-index, but I could not just set my brain to use 5 0 for my birthday[Now that you know when my birthday is, you can send me birthday cards or presents and I do not mind receiving them before of after my bithday…so it’s still not too late 🙂 ]

Happy birthday, Java8!

And for you: hope you all have dates as good as the Java 8 ones!