1

Is there some simple way to parse different type of dates in java without playing with the date strings?

I have to deal with 6 different types of date:

  • 16 May 2013 19:27:12 CEST
  • Tue, 14 May 2013 13:15:00 +0200
  • 2013-05-20T12:01:57Z
  • 13/11/2012 15:30:00
  • 11.11.2013
  • 1970-01-01T00:00:00.000Z

I don't know if a date will arrive in one format or the other from the server...

thank you!

aveschini
  • 1,632
  • 3
  • 22
  • 39
  • 2
    check out JodaTime http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormatter.html#parseDateTime%28java.lang.String%29 – hovanessyan May 20 '13 at 12:21
  • I want to keep the package size as small as possible so I don't think I'm going to use an external library only for dates. Thank you anyway! This is a good answer! – aveschini May 20 '13 at 13:15

5 Answers5

4

You can iterate over all formats and try to parse, ignore the exception. If no format fits, throw an exception.

similar answer

Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1

The best-known alternative to the standard API is Joda-Time. Also JSR 310 an improved Date/Time API for Java SE 7

sivan
  • 79
  • 4
  • This is a good answer but I should keep the package size as small as possible so I don't think I'm going to use external libraries only for dates. Thank you anyway of course1 – aveschini May 20 '13 at 13:14
1

I don't think there's an actual "easy" way to deal with such different date formats. If you have the option to nail a "standard date format" to the server that would be the easy way.

A common approach is to build a DateParser for every 'freaky' date format that you have to deal with. Here's an example for your first date, using Joda Time:

String date1 = "16 May 2013 19:27:12 CEST";
        DateTimeFormatter fmt = new DateTimeFormatterBuilder()
            .appendDayOfMonth(2)
            .appendLiteral(' ')
            .appendMonthOfYearShortText()
            .appendLiteral(' ')
            .appendYear(4, 4)
            .appendLiteral(' ')
            .appendHourOfDay(2)
            .appendLiteral(":")
            .appendMinuteOfDay(2)
            .appendLiteral(":")
            .appendSecondOfDay(2)
            .appendLiteral(' ')
            .appendLiteral("CEST")
            .toFormatter();

        DateTime parseDateTime = fmt.parseDateTime(date1);

        System.out.println(parseDateTime);

I hope this will help you build DateParsers for the other cases. And remember - always save your dates in relation to UTC!

hovanessyan
  • 30,580
  • 6
  • 55
  • 83
1

To elaborate on the stock "Use Joda-Time" answer, in Joda-Time you can create a DateTimeParser for each of your 6 formats, then append them to a single DateTimeFormatter.

See the accepted answer to this question: Using Joda Date & Time API to parse multiple formats

Community
  • 1
  • 1
GenericJon
  • 8,746
  • 4
  • 39
  • 50
0

Here is the sample code, for date string like 2001-07-04T12:08:56.235-0700 we need to use following format "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = simpleDateFormat.parse("2001-07-04T12:08:56.235-0700");
System.out.println(date);

details are available in below mentioned link

SimpleDateFormat

Zeeshan Bilal
  • 1,147
  • 1
  • 8
  • 22