1

I'm trying to execute this code:

Date date = null;
if (detailsBean.getDiscoveryProjectBean ().getCreatedDate ()==null || 
    detailsBean.getDiscoveryProjectBean ().getCreatedDate ().equalsIgnoreCase(""))
{
    projectDetails.getDiscoveryProject().setCreationTime(new Date());
}
else
{
    try 
    {
        date = new SimpleDateFormat (FormatUtils.simpleFormat).
            parse (detailsBean.getDiscoveryProjectBean ().getCreatedDate ());
    } catch (Exception e) {
        throw new PanDaApplicationException (e.getMessage ());
    }
    projectDetails.getDiscoveryProject().setCreationTime(date);
}

in the try block a ParseException exception is thrown. I don't know the cause of that as the code seems fine, however. the definition of the FormatUtils.simpleFormat is public static final String simpleFormat = "dd-MMM-yyyy" and detailsBean.getDiscoveryProjectBean().getCreatedDate() have value like 28-Feb-2013

I really don't have any clues why this exception is thrown and I need help.

5
  • 1
    A value like "28-Feb-2013" or exactly that? And what's your default locale? Commented Mar 3, 2013 at 9:23
  • I suppose your default local is not English you have to change it to en Commented Mar 3, 2013 at 9:25
  • Exception message should contain date string the parser failed to parse. Could you please show us exception message? Commented Mar 3, 2013 at 9:25
  • @JonSkeet well exactly that. Commented Mar 3, 2013 at 10:17
  • Thanks guys, yes I needed to add locale.US, I think that someone in my team changed it. Commented Mar 3, 2013 at 15:12

2 Answers 2

4

My guess is that the problem is the way that SimpleDateFormat uses your default locale - if your locale doesn't use "Feb" as an abbreviated month name, you'll have problems. So if all your data is actually in English, you might want:

DateFormat format = new SimpleDateFormat(FormatUtils.simpleFormat, Locale.US);
format.setTimeZone(...); // See below
date = format.parse(detailsBean.getDiscoveryProjectBean().getCreatedDate());

Note the part about setting the time zone. Again, SimpleDateFormat will use your system default if you don't specify anything else. (You'll get the instant of "midnight in the specified time zone" as the Date value.)

I would also strongly urge you to consider using Joda Time instead of the built-in Date/Calendar types - it's a much better date/time API.

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

Comments

3
Locale.setDefault (Locale.ROOT);
System.out.println (new SimpleDateFormat ("dd-MMM-yyyy").parse ("28-Feb-2013"));
Locale.setDefault (Locale.forLanguageTag ("ru"));
System.out.println (new SimpleDateFormat ("dd-MMM-yyyy").parse ("28-Feb-2013"));

For me output is:

Thu Feb 28 00:00:00 MSK 2013
Exception in thread "main" java.text.ParseException: Unparseable date: "28-Feb-2013"
    at java.text.DateFormat.parse(DateFormat.java:357)
    at DateFormat.main(DateFormat.java:19)

So the same date successfully parsed with ROOT locale, but failed with Russian.

3 Comments

It's generally a much better idea (IMO) to just specify the locale for the particular SimpleDateFormat, rather than change the default.
@JonSkeet I just demonstrated how improper default locale can cause such exception. Of cause there are several ways how to fix this.
The trouble is, when you only mention one of these, it's reasonable for the OP to infer that you believe that's the best way of doing it.

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.