Friday, 24 January 2014

Live Coding on Java 8 Date Time API

Yesterday I recorded a live coding session on java.time. Below is all the information so you can code along if you like:

The Video

The Source Code

All the source code is freely available on github you can get this running the following command:

The (brief) details for setting up the IDE can be found in the readme.

The Slides

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!

Saturday, 18 January 2014

User Group Visit to San Francisco

Why am I in San Francisco? 

Is it:
  • A micro brewery pub crawl?
  • The fact it's almost 20°C tomorrow?
  • To attend a Brazilian BBQ in January? 
These things are certainly going to be a nice bonus of being here, however that's not the main reason. I'm here representing the London Java Community at Oracle's big annual user group leaders convention. The purpose of the conference is to bring leaders of a variety of different Oracle user groups together to discuss best practices and drive improvement of communities and involvement for all those concerned. Particularly this year the topics I am interested in and will be participating in are:
  • Using social media - I'm particularly interested in discussing handling negative discussion and feedback via this extremely public forum.
  • Turning members into leaders and growing speaker talent. This has been an active stream in the LJC and Trisha Gee and team have been doing a fantastic job of running workshop and helping people gain the courage to make that first talk. 
  • Gamifiying your user group experience and improving user group content. I'm mainly interested in this because I get motivated by badges and awards for certain things. I think it could put a fun spin on how we run certain events in the LJC.
I plan to blog about interesting conversations and what comes out of these themes over the course of the week. The summit will only be the beginning of the discussions and I look forward to seeing more about what our members think to some of the ideas and which may work for us. 

When discussing any kind of conference and benefits of conferences I always say that it doesn't have to be a specific talk that motivates you but a catalyst to change. All the major turning points in my career have been down to ideas gained at conferences. It will be interesting to be involved in a non technical conference across a range of different products, experiences and backgrounds. 

I have very kindly had partial sponsorship from the LJC and the JCP Program Office to be here this week and I can't thank them both enough for the opportunity. 

I am now a JCP Executive Committee Representative

Why are the JCP helping to sponsor my visit here? After 3 years of working on JSRs with the London Java Community with the LJC JCP committee I have now stepped up my involvement and represent the LJC directly with other JCP executive committee members. I am attending my first face-to-face meeting at the end of this week. I'm really thrilled to be working with so many fantastic organisations and individuals to help maintain and form standards within Java. As the meeting has official minutes it's unlikely I will post anything further.