0

I have this weird date format that I have to parse.

2015-12-18T03:36:06.000+0000

I am currently mapping a regex to date formats so I can parse different dates. However, this format got me confused. Any help appreciated.

safaiyeh
  • 1,655
  • 3
  • 16
  • 35
  • I'm currently reading this question but i need help in understand what is your problem. Any help appreciated. Are you looking for a regular expression for the date ? or you want to know how you can parse this ? – StackFlowed Dec 28 '15 at 19:46
  • Joda time has formatting options that can handle this. – Untitled123 Dec 28 '15 at 19:47
  • @StackFlowed I am trying to parse the date format however I cannot figure out the regex or the format it is in for SimpleDateFormat – safaiyeh Dec 28 '15 at 19:48
  • 6
    It's not weird, its [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) – cheb1k4 Dec 28 '15 at 19:50
  • 4
    [Converting ISO-8601 compliant string to date](http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date). – Mick Mnemonic Dec 28 '15 at 19:53
  • It not just not weird, it also been asked before, several times if I recall – MadProgrammer Dec 28 '15 at 20:31

1 Answers1

2

To parse a String into a Date in Java, you use a DateFormat object, and specify the format the date is in. There is no need to use a Regex, the Java library has a way to do this for you.

final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
final Date d = df.parse("2015-12-18T03:36:06.000+0000");  // From your code above
System.out.println(d);

See the JavaDoc for SimpleDateFormat for more explanation as to what the symbols mean. This is actually a common format for dates called ISO 8601, I just took the pattern right from the documentation.

Watch out! These DateFormat objects are not threadsafe.

Todd
  • 30,472
  • 11
  • 81
  • 89
  • 2
    A better solution probably be to use the new date/time API – MadProgrammer Dec 28 '15 at 20:31
  • [An answer by Adam](http://stackoverflow.com/a/27479533/642706) using the new date/time API (java.time) can be found on [the question](http://stackoverflow.com/q/2201925/642706) of which this question is a duplicate. – Basil Bourque Jan 11 '16 at 07:04