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

This post has a good discussion on comparing the Java Date/Time API vs JODA.

I personally just use Gregorian Calendar and SimpleDateFormat any time I need to manipulate dates/times in Java. I've never really had any problems in using the Java API and find it quite easy to use, so have not really looked into any alternatives.


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.


The Apache Commons Lang project has a DateUtils class that performs helpful Date operations.

I use DateUtils.truncate() a lot, which will "zero out" parts of the Date for you (helpful if you want your Date object to, say, represent a date and not include any time information). Each method works for both Date and Calendar objects too.

http://commons.apache.org/lang/

Tags:

Datetime

Java