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!

2
  • 2
    check out JodaTime joda-time.sourceforge.net/apidocs/org/joda/time/format/… Commented May 20, 2013 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! Commented May 20, 2013 at 13:15

5 Answers 5

4

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

similar answer

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I think this is the best answer because I didn't specify the package size issue. Thank you!
1

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

1 Comment

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
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!

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.