1

Is there an easy way to parse the following 2 types of String to Date in Java

2013-11-22T18:37:55.645+0000
2013-11-22T14:20:30.645Z

Both mean the same thing, but I am having to use 2 different date format patterns

yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

and I want to do it with only 1 pattern.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Thunderhashy
  • 5,291
  • 13
  • 43
  • 47

3 Answers3

1

Yes, there is a very simple way called SimpleDateFormat. Send your format to constructor of this class and format date according to your spec.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    Of course SimpleDateFormat, but I would have to call it 2 times with the 2 formats. I wanted a single format which could match both patterns – Thunderhashy Nov 25 '13 at 20:04
  • Unfortunatly `SimpleDateFormat` does not support such option. It seems that you offer here an idea of `PatternDateFormat` that accepts patterns and therefore can match several formats. You are welcome to implement such one. It sounds interesting and pretty useful! – AlexR Nov 25 '13 at 21:00
1

Both of your format patterns are variations of the same, ISO 8601.

Easy in Joda-Time 2.3.

One line of code, using Joda-Time's built-in ISO 8601 formatter. That formatter handles both offsets, either zeros or a Z.

org.joda.time.format.ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime( eitherStringGoesHere );

More detailed code…

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

String dateTimeStringZero = "2013-11-22T18:37:55.645+0000";
String dateTimeStringZulu = "2013-11-22T18:37:55.645Z";

org.joda.time.DateTime dateTimeZero = org.joda.time.format.ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime( dateTimeStringZero );
org.joda.time.DateTime dateTimeZulu = org.joda.time.format.ISODateTimeFormat.dateTime().withZoneUTC().parseDateTime( dateTimeStringZulu );

Output…

System.out.println( "dateTimeZero: " + dateTimeZero );
System.out.println( "dateTimeZulu: " + dateTimeZulu );

When run…

dateTimeZero: 2013-11-22T18:37:55.645Z
dateTimeZulu: 2013-11-22T18:37:55.645Z

If you want a time zoned DateTime, change out the withZoneUTC(). See the withZone method. For user’s default time zone, simply omit any time zone call.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • This looks exactly what I wanted.. Unfortunately it means I need to bring in joda jar file. I thought I could do without it, but there seems no way. – Thunderhashy Nov 26 '13 at 16:07
  • @Harsha Joda-Time is the first thing I add to a new project (even before [SLF4J](http://www.slf4j.org/) & [LogBack](http://logback.qos.ch/)). Unfortunately, the java.util.Date/Calendar classes bundled with Java are notoriously bad, the worst part of the Java platform. That's why they are being supplanted by [JSR 310: Date and Time API](http://jcp.org/en/jsr/detail?id=310) (java.time.*) in Java 8. JSR 310 is inspired by Joda-Time, but entirely re-architected, not directly built on Joda-Time. – Basil Bourque Nov 26 '13 at 18:34
0

Have you tried this:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    Date dt1 = sdf.parse(str1);
chaos
  • 641
  • 10
  • 21