Guide to Time and Date in Java (Part 3)

DZone 's Guide to

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 ·
Free Resource

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:

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:

LocalTime.of(20, 0, 0)

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

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.


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)

    def '#date +/- 1 month gives back the same date'() {
            date == date.plusMonths(1).minusMonths(1)
            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

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

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


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.


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.

date and time ,java ,testing ,time ,time zones

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}