Dedyshka
Dedyshka

Reputation: 471

Unparseable date

I have a string-date "31-Dec", and pattern "dd-MMM". And the next code

DateFormat formatter = new SimpleDateFormat(pattern);
formatter.setTimeZone(timeZone);
formatter.parse(input);

generates exception

java.text.ParseException: Unparseable date: "31-Dec"
    at java.text.DateFormat.parse(DateFormat.java:337)....

What did I do wrong?

Thanks!

Upvotes: 2

Views: 3274

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 338211

tl;dr

MonthDay
    .parse(
        "31-Dec" ,
        DateTimeFormatter
            .ofPattern ( "dd-MMM" )
            .withLocale ( Locale.US )
    )

MonthDay

string-date "31-Dec", and pattern "dd-MMM"

There's a class for that: MonthDay in the java.time framework in Java 8+.

You were using the Calendar class, which represents a moment, a point on the timeline. But your input is not a moment, it is merely a month and day-of-month. So your input lacks the necessary information to make a Calendar instance. Furthermore, Calendar is a terribly flawed class that was years ago supplanted by the modern java.time classes defined in JSR 310.

To parse your input text as a MonthDay object, define a formatting pattern in a DateTimeFormatter object. Be sure to specify the Locale by which to understand the localized name of month.

String input = "31-Dec";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "dd-MMM" ).withLocale ( Locale.US );
MonthDay md = MonthDay.parse ( input , f );

ISO 8601

Generate text representing that value in standard ISO 8601 format. The first hyphen in the result represents the omitted year.

md.toString() = --12-31

The java.time classes use the ISO 8601 formats by default when parsing/generating text. So no need to specify a formatting pattern.

String standardText = md.toString ( );
MonthDay md2 = MonthDay.parse ( standardText );

I suggest you educate the publisher of your data about using only the standard ISO 8601 formats for exchanging date-time values as text.

Upvotes: 0

brimborium
brimborium

Reputation: 9512

One problem could be that your Locale is not english. Try this:

DateFormat formatter = new SimpleDateFormat("dd-MMM", Locale.ENGLISH);
try {
    System.out.println(formatter.parse("31-Dec"));
} catch (ParseException e) {
    e.printStackTrace();
}

This returns for me:

Thu Dec 31 00:00:00 CET 1970

As you are missing a year in your date string, you see that it automatically inserts 1970 as year.

Upvotes: 5

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

I wrote up a simple example that works on my machine. Give it a shot on yours, it may help you pinpoint the problem.

public class Example {

    public static void main(String[] args) {
        String data = "31-Dec";
        String pattern = "dd-MMMM";

        DateFormat formatter = new SimpleDateFormat(pattern);
        formatter.setTimeZone(TimeZone.getDefault());
        try {
            Date date = formatter.parse(data);
            System.out.println(date.getDate());
            System.out.println(date.getMonth());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

Debobroto Das
Debobroto Das

Reputation: 862

Try this---

    String pattern = "dd-MMM";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
dateFormat.setTimeZone(TimeZone.getDefault());
String input = "31-Dec";
try
{
    dateFormat.parse(input);
    Calendar cal = dateFormat.getCalendar();
    System.out.println("Day "+cal.get(Calendar.DAY_OF_MONTH));
    System.out.println("MONTH "+ cal.get(Calendar.MONTH));

}catch(Exception e)
{

}

Upvotes: 0

Debobroto Das
Debobroto Das

Reputation: 862

I think , Your input string (representing date ) is not in the format described in the pattern.

Upvotes: 0

Related Questions