Dragos Pitica

z is for zulu

Zulu or Zulu Time is a term for Universal Time Coordinated (UTC) used in military and navigation. In military shorthand, the letter Z follows a time expressed in Greenwich Time such as 08:00Z or 0800Z. This is spoken as “zero eight hundred Zulu”.

Greenwich Time is determined by a longitude of 0 degrees 0 minutes – the prime meridian that separates East from West in the world geographical system. This line of longitude is based near the British Naval Observatory in Greenwich, England, a great place to visit as well.

capitalization

Working every day with broadcasts, handling dates and times is just one of the many important and tricky tasks. Given how often operations of parsing and formatting are not correct, I thought it would be handy to list some of the most common issues.

Pattern capitalization issue usually comes up due to copying the pattern from another piece of code without actually knowing what it does. For example, it’s pretty clear what YYYY-MM-DD HH:MM:SS means as a date/time format, however, it doesn’t mean it’s the right pattern to put in code. The most common mistakes here are:

  • Using mm for both months and minutes or MM for minutes and mm for months, rather than the other way around.
  • Using hh for “hour of day” when HH is intended. H is in the range 0-23 and usually represented as “05” for example. h is in the range 1-12 and almost always represented along with an AM/PM specifier because otherwise, it’d get ambiguous.

don’t be lazy

Also, a general, but very important aspect of programming is that you should never copy or add code from elsewhere into your own codebase without understanding why and how it works. In the next example, the pattern and the actual data are entirely different. It’s obvious that the author has copied the pattern from somewhere else without thinking what the magic string yyyy-MM-dd'T'HH:mm:ss is there for.


time zones

The first misunderstanding when working with time zones is to assume that a UTC offset (e.g. “+3 hours”) represents an actual time zone. The UTC offset is the difference between UTC and local time. For example, at this moment, I’m in the UK time zone which is currently at UTC+0. However, in the summer, in the same time zone, it will be at UTC+1.

Therefore, a value of (say) “2017-05-17T13:34:00+0100” only tells you the UTC offset – it doesn’t tell you the time zone because there may be other time zones with different offsets at differing times. Moreover, not sharing this explanation on timezones just makes this blog post silly.

your time is up

Joda-Time became the de facto standard date and time library for most Java people and they started to use this third party library due to poor standard date and time classes prior to Java SE 8. Now with Java 8 out, the author of Joda-time himself made a couple of official statements on their website that sound like this:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) – a core part of the JDK which replaces this project.
Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

With this said, I’ll leave you with some basic and regularly used scenarios of how to do Date and Time in Java 8.


Date/time handling in Java is a task filled with many problems sometimes, as you can see after a simple look at Stack Overflow. However, read the documentation carefully, make sure you know exactly what you’re converting from and to and don’t blindly copy pasta code that you don’t understand.

If you enjoyed the read, drop us a comment below or share the article, follow us on Twitter or subscribe to our #MetaBeers newsletter. Before you go, grab a PDF of the article, and let us know if it’s time we worked together.

blog comments powered by Disqus