I wanna parse a date that comes in this format "Tue Dec 22 19:16:57 2015 UTC" in java Is this a valid UTC DateTime? And how to parse it? I tried :
DateFormat format = new SimpleDateFormat("ddd MMM dd HH:mm:ss yyyy UTC");
I wanna parse a date that comes in this format "Tue Dec 22 19:16:57 2015 UTC" in java Is this a valid UTC DateTime? And how to parse it? I tried :
DateFormat format = new SimpleDateFormat("ddd MMM dd HH:mm:ss yyyy UTC");
Consulting the SimpleDateFormat
documentation, we see that:
The format specifier for the name of the weekday is E
, not d
The format specifier for a timezone indicator (UTC being a timezone indicator of sorts) is Z
for RFC-822 timezones (or X
for ISO-8601 timezones, or z
for "general" timezones)
So the string is "EEE MMM dd HH:mm:ss yyyy Z"
. Live Example on IDEone
However, the documentation doesn't say that UTC
will be recognized as a timezone (either with Z
or X
or z
), so you may want to pre-process the string to change UTC
to one of the specifiers supported by RFC-822:
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z");
String s = "Tue Dec 22 19:16:57 2015 UTC";
s = s.replace("UTC", "GMT");
Date d = format.parse(s);
If your locale isn't English by default, you'll also need to tell SimpleDateFormat
to use English (since those are English day and month names):
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z", Locale.ENGLISH);
Not sure how you have your code written, but one way to display it in Military time, you could do this:
return String.format("%02d:%02d:%02d", hour, minute, second); //The first argument being key. and the second argument being what is going to be displayed in each "%02d" Will be displayed as 00:00:00
Hope this helps. :-)