Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to manipulate Dates and Timestamps in Java?

Tags:

Every time I need to work with date and/or timstamps in Java I always feel like I'm doing something wrong and spend endless hours trying to find a better way of working with the APIs without having to code my own Date and Time utility classes. Here's a couple of annoying things I just ran into:

  • 0-based months. I realize that best practice is to use Calendar.SEPTEMBER instead of 8, but it's annoying that 8 represents September and not August.

  • Getting a date without a timestamp. I always need the utility that Zeros out the timestamp portion of the date.

  • I know there's other issues I've had in the past, but can't recall. Feel free to add more in your responses.

So, my question is ... What third party APIs do you use to simplify Java's usage of Date and Time manipulation, if any? Any thoughts on using Joda? Anyone looked closer at JSR-310 Date and Time API?

like image 410
Joe Dean Avatar asked Sep 17 '08 21:09

Joe Dean


People also ask

How do I change the format of a timestamp in java?

We can format the Timestamp value using SimpleDateFormat class. Initially, by using the Timestamp class, the time is getting displayed in a standard format, but we can format it to our own choice using SimpleDateFormat class.


1 Answers

java.time

Java 8 and later now includes the java.time framework. Inspired by Joda-Time, defined by JSR 310, extended by the ThreeTen-Extra project. See the Tutorial.

This framework supplants the old java.util.Date/.Calendar classes. Conversion methods let you convert back and forth to work with old code not yet updated for the java.time types.

Table of date-time types in Java, both modern and legacy

The core classes are:

  • Instant
    A moment on the timeline, always in UTC.
  • ZoneId
    A time zone. The subclass ZoneOffset includes a constant for UTC.
  • ZonedDateTime = Instant + ZoneId
    Represents a moment on the timeline adjusted into a specific time zone.

This framework solves the couple of problems you listed.

0-based months

Month numbers are 1-12 in java.time.

Even better, an Enum (Month) provides an object instance for each month of the year. So you need not depend on "magic" numbers in your code like 9 or 10.

if ( theMonth.equals ( Month.OCTOBER ) ) {  …

Furthermore, that enum includes some handy utility methods such as getting a month’s localized name.

If not yet familiar with Java enums, read the Tutorial and study up. They are surprisingly handy and powerful.

A date without a time

The LocalDate class represents a date-only value, without time-of-day, without time zone.

LocalDate localDate = LocalDate.parse( "2015-01-02" );

Note that determining a date requires a time zone. A new day dawns earlier in Paris than in Montréal where it is still ‘yesterday’. The ZoneId class represents a time zone.

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );

Similarly, there is a LocalTime class for a time-of-day not yet tied to a date or time zone.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

like image 184
Basil Bourque Avatar answered Sep 23 '22 04:09

Basil Bourque