Skip to content

Instantly share code, notes, and snippets.

@sckirk
Forked from ebouchut/formatDateAsUTC.java
Last active October 3, 2019 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sckirk/9322db3dcf4b3f91d19021e3e13d0853 to your computer and use it in GitHub Desktop.
Save sckirk/9322db3dcf4b3f91d19021e3e13d0853 to your computer and use it in GitHub Desktop.
Format a Java Date as UTC String: yyyy-mm-dd HH:mm:ss'Z'
publicString formatDateAsUTC(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.format(date);
}
@sckirk
Copy link
Author

sckirk commented Oct 3, 2019

Updated bug: I was playing around with this Java code and was confused why the minutes were showing up as the month. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html confirmed the month needs to be 'M' instead of 'm'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment