-
-
Save ufosaga/4957fa7d64bc116119961950d4c8d380 to your computer and use it in GitHub Desktop.
Convert Date String to/from ISO 8601 respecting UTC in Java
This file contains 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment