Monday, 20 January 2014

Java 8 Date Time: Happy Martin Luther King Day

Today is Martin Luther King Day in the US. I had totally forgotten about this holiday until I saw a few tweets this morning about it. The interesting thing about this holiday is that rather than being on a specific day, it is on the third Monday of January.

As I'm currently preparing some JSR-310 material, I thought it would be interesting to share (and discuss) TemporalQuery - a new interface in Java 8 that makes it possible to write custom queries against the new DateTime classes. There is often a use case in business that an event happens that is unique to a particular domain, an example would be Futures market rolls of contracts or time periods of validity.

TemporalQuery allows us to capture the logic to perform an operation against a temporal class and return an object representing what we were querying. The interface is generic so we can choose what we return. This is a really neat design feature in Java 8 allowing us to externalise logic outside of the core API, but encapsulate the functionality as a data operation.

TemporalQuery is also a FunctionalInterface so it can be used as a lambda on a stream of temporals. In the example below we just consider one date. To find the next Martin Luther King day we can take the current date and perform the following: NextMartinLutherKingDayQuery());

The NextMartinLutherKingDayQuery is our implementation of a TemporalQuery:

Here are some things to note about the implementation:

  • In the private method I take a date at the beginning of January of the year passed. Using a TemporalAdjuster I manipulate that date to cycle it forward to where I want to be. TemporalAdjusters add more power than just plusDays or minusMonths to allow us to work on more expressive situations with date and time. 
    • The TemporalAdjusters class has some really nice methods as used here, or you can write your own TemporalAdjuster if it doesn't exist. 
    • NextOrSame is useful for this type of operation where we don't know what day we are on, but if it's the day we are querying we don't want to advance the day. We use it to iterate to the third Monday of the month of January in the year in question.
  • I then use a the Period class to find out from the date passed whether Martin Luther King Day has passed, or it is today. If it has passed I return next year's date, otherwise returning this year's date. Period is a nice abstraction so I don't have to start subtracting millisecond values etc - yes we've all seen it. 
If you're interested in playing around with this I suggest downloading the latest Java 8 beta and having a go. 

Happy Martin Luther King Day!

1 comment:

  1. There is also the dayOfWeekInMonth() adjuster on TemporalAdjusters -

    return LocalDate.of(year, JANUARY, 1).with(dayOfWeekInMonth(3, MONDAY));