Over a million developers have joined DZone.

Guide to Time and Date in Java (Part 3)

In part three, Tomasz Nurkiewicz continues to provide insight into several time and date tools for Java, and how to avoid major issues with building time into applications. This post focuses on how to express local time and date without reference to any specific time zone and ways of testing dates.

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

In part three of this post, we'll discuss how to express local time and date without reference to any specific time zone and discussed ways of testing dates. I recommend reading parts one and two if you haven't already.

This post continues to cover the basics of handling time and date in Java, but the scope of this article does not extend to cover every possible situation... this is just a start. 

Local Time and Date

Sometimes you want to express date or time without any specific time zone. For example, my birthday is:

//1985-12-25
LocalDate.of(1985, Month.DECEMBER, 25)


I will celebrate my birthday that day no matter where I am. This means the party will start at approximately:

//20:00
LocalTime.of(20, 0, 0)


...irrespective to time zone. I can even say that my birthday party this year will be precisely at:

//2016-12-25T20:00
LocalDateTime party = LocalDateTime.of(
        LocalDate.of(2016, Month.DECEMBER, 25),
        LocalTime.of(20, 0, 0)
);


But, as long as I don't provide you with a location, you don't know what the time zone I live in is, and thus don't know what the actual start time is. It's impossible (or very foolish) to convert from LocalDateTime to Instant or ZoneDateTime (which both point to a precise moment in time) without giving a time zone. So, local times are useful, but they don't really represent any moment in time.

Testing

I just scratched the surface of pitfalls and issues one might have with time and date. For example, we didn't cover leap years which can become a serious source of bugs. I find property-based testing extremely useful when testing dates:

import spock.lang.Specification
import spock.lang.Unroll

import java.time.*

class PlusMinusMonthSpec extends Specification {

    static final LocalDate START_DATE =
            LocalDate.of(2016, Month.JANUARY, 1)

    @Unroll
    def '#date +/- 1 month gives back the same date'() {
        expect:
            date == date.plusMonths(1).minusMonths(1)
        where:
            date << (0..365).collect {
                day -> START_DATE.plusDays(day)
            }
    }

}


This test makes sure adding and subtracting one month to any date in 2016 gives back the same date. Pretty straightforward, right? This test fails for a number of days:

date == date.plusMonths(1).minusMonths(1)
|    |  |    |             |
|    |  |    2016-02-29    2016-01-29
|    |  2016-01-30
|    false
2016-01-30


date == date.plusMonths(1).minusMonths(1)
|    |  |    |             |
|    |  |    2016-02-29    2016-01-29
|    |  2016-01-31
|    false
2016-01-31


date == date.plusMonths(1).minusMonths(1)
|    |  |    |             |
|    |  |    2016-04-30    2016-03-30
|    |  2016-03-31
|    false
2016-03-31

...


Leap years cause all sorts of issues and break the laws of math. Another similar example is adding two months to a date that is not always equal to adding one month two times.

Summary

Yet again, this series is just an intro to handling dates and times in Java—it is meant to summarize some of the common problems/solutions, but by no means does it cover every situation you may run into. In this section, we went over how to express local time and date without reference to any specific time zone and discussed a few common ways of testing dates.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:
java ,date and time ,time ,time zones ,testing

Published at DZone with permission of Tomasz Nurkiewicz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}