-2

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");
Joseph Young
  • 2,758
  • 12
  • 23
Ahmet Oğruz
  • 17
  • 1
  • 6
  • 3
    With respect (to the number of times this type of question gets asked), you get out the [JavaDocs](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) and check the available specifications, you do some research into what other people have done and you try a number of approaches, if you really think you can't find the answer, which happens, you let use know what else you've tried (and *"I've tried everything"* isn't what we want to hear, maybe show use the formats you've tried, for example) – MadProgrammer Dec 28 '15 at 09:22

3 Answers3

1

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);

Live Example

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);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • it gives an exception ; ** Unparseable date: "Tue Dec 22 19:16:57 2015 GMT" ** – Ahmet Oğruz Dec 28 '15 at 09:18
  • @AhmetOğruz: No, it doesn't -- see the live examples. – T.J. Crowder Dec 28 '15 at 09:21
  • 1
    @AhmetOğruz Concur with TJ, it works and `System.out.println(d);` prints `Wed Dec 23 06:16:57 EST 2015` for me – MadProgrammer Dec 28 '15 at 09:24
  • [link] http://oi67.tinypic.com/j67k08.jpg – Ahmet Oğruz Dec 28 '15 at 09:29
  • I upadted as below and it works, DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z",Locale.ENGLISH); – Ahmet Oğruz Dec 28 '15 at 09:40
  • @AhmetOğruz: Well of course. If your locale isn't English (which you didn't mention), how would you expect `SimpleDateFormat` to parse English day and month names?! I've added that information to the answer, but the answer was correct in the first place based on the information actually in the question. – T.J. Crowder Dec 28 '15 at 09:41
0

Always refer to javadoc for additional information.

 DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z");
sidgate
  • 14,650
  • 11
  • 68
  • 119
-1

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. :-)