Created
November 18, 2013 13:39
-
-
Save nickrussler/7527851 to your computer and use it in GitHub Desktop.
Convert Date String to/from ISO 8601 respecting UTC in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String toISO8601UTC(Date date) { | |
TimeZone tz = TimeZone.getTimeZone("UTC"); | |
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); | |
df.setTimeZone(tz); | |
return df.format(date); | |
} | |
public static Date fromISO8601UTC(String dateStr) { | |
TimeZone tz = TimeZone.getTimeZone("UTC"); | |
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); | |
df.setTimeZone(tz); | |
try { | |
return df.parse(dateStr); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} |
Thank you for this, very helpful.
Thanks a lot !
+1
Thanks for the snippet
+1
Thanks for the gist
+1
Дякую
+1
Thank you!
Found this while googling "java date parse from utc". Other answers usually don't set timezone for SimpleDateFormat.
thank you very much
df.setTimeZone(...)
thanks
Why we need to set timezone. Things are working perfectly without it.
Thanks, life saver. Someday I'll get my project off the Calendar
/Date
classes!
Гарно дякую)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this, very helpful.