Home >Java >javaTutorial >Why Does SimpleDateFormat('yyyy-MM-dd'T'HH:mm:ss'Z'') Sometimes Show IST Instead of GMT?
Confusion in SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") Timezone Interpretation
In java, SimpleDateFormat allows users to parse and format dates using a specified pattern. When using the pattern "yyyy-MM-dd'T'HH:mm:ss'Z'", the expectation is that the 'Z' represents the GMT/UTC timezone. However, there have been concerns that the resulting date shows the IST timezone instead of the expected GMT timezone.
Explanation
The 'Z' character in the pattern indeed signifies the GMT/UTC timezone. However, it is crucial to note that simply adding the 'Z' to the date/time string does not magically change the value. The timezone must be explicitly set for the SimpleDateFormat object using the setTimeZone method.
Resolving the Issue
To ensure that the output date reflects the GMT/UTC timezone correctly, one needs to explicitly set the timezone to GMT using the following line of code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
By setting the timezone, the SimpleDateFormat object will correctly interpret the 'Z' as GMT/UTC and display the date in the desired timezone.
The above is the detailed content of Why Does SimpleDateFormat('yyyy-MM-dd'T'HH:mm:ss'Z'') Sometimes Show IST Instead of GMT?. For more information, please follow other related articles on the PHP Chinese website!