Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST
Your SimpleDateFormat
object is confusing 'Z'
for your local timezone (such as IST), and it should be UTC. Instead, implement UTC like this:
To confidently handle this in modern versions of Java, use the java.time
package:
For accurate UTC management, TimeZone.getTimeZone("UTC")
and Instant.parse()
, never fail.
Understanding 'Z'
vs TimeZone
The date and time pattern "yyyy-MM-dd'T'HH:mm:ss'Z'" is ideally crafted to represent a UTC timestamp. However, the role 'Z' plays here can be deceiving. It seems to direct you towards the Zulu time zone (essentially UTC), but without properly setting the timezone, SimpleDateFormat
will resort to the system's timezone by default.
So in reality, employing 'Z'
barely changes the timezone. It's crucial to set the TimeZone for your SimpleDateFormat
object in a manner like this:
Accommodating various time zones
'X' for flexible time zone handling
When handling ISO 8601 time zones, you can use 'X'
at the end of the pattern for flexible adaptation:
Using java.time
for modern Java
The java.time
package is designed to cater to ISO 8601 in a broader and more intuitive manner:
The conversion between time zones
You can convert a UTC Instant
to a different time zone with atZone
:
📌 Key Tip: java.time
is always a better choice in modern Java development. If SimpleDateFormat
is your only option, never forget to set the time zone explicitly.
Overcoming complications
Remember to tackle the following issues while dealing with time zones:
- Inconsistent Behaviour: Always test your software thoroughly as different JDK versions may display different behaviours towards timezone management.
- Confusing Patterns: Patterns like "yyyy-MM-dd'T'HH:mm:ss'Z'" can be misleading. Using 'X' for the time zone parsing or switching to
java.time
is safer. - The Old API:
TimeZone
andSimpleDateFormat
belong to the older Java Date API and can be less intuitive and often lead to errors.
Was this article helpful?