user2532344
user2532344

Reputation: 73

Unexpected exception while parsing date

I am trying to parse the date according to the following code but getting exception. Below is the code -

public class DateTest {
    public static void main(String args []) {
        String start = "23-Jan-2017";
        DateFormat dateFormatTripStartDate = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");

        try {
            Date parsedDate = dateFormatTripStartDate.parse(start);
            System.out.println(parsedDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Exception :

java.text.ParseException: Unparseable date: "23-Jan-2017"
    at java.text.DateFormat.parse(DateFormat.java:357)
    at DateTest.main(DateTest.java:18)

Kindly help me identify the problem. Thanks.

Upvotes: 3

Views: 741

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338181

tl;dr

LocalDate.parse( 
    "23-Jan-2017" , 
    DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US )
)

Using java.time

Other Answers are correct about formatting pattern mismatching input data. But both the Question and other Answers are outdated.

The modern way is with java.time classes that supplant the troublesome old date-time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US );
LocalDate ld = LocalDate.parse( "23-Jan-2017" , f );

ld.toString(): 2017-01-23

Specify the Locale as that determines the human language used to translate the name of the month. If omitted the JVM’s current default Locale is used implicitly. That default can be changed at any moment by any code in any thread of any app within the JVM, so do not rely upon it.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, andfz more.

Upvotes: 2

SachinSarawgi
SachinSarawgi

Reputation: 2692

You are using pattern "dd-MMM-yyyy hh:mm a". But in actual "hh:mm a" part is not present in the "23-Jan-2017" value. Because of this parse function is not able to parse the String date.

So change your pattern to "dd-MMM-yyyy" which matches your date string. This will remove the exception you are getting.

Upvotes: 0

Michael Gantman
Michael Gantman

Reputation: 7792

First of all, the answer from kamehl23 is correct. Your parsed string may not be missing any parst that are specified in format and thus you will need to modify your format to DateFormat dateFormatTripStartDate = new SimpleDateFormat("dd-MMM-yyyy"); However just to add few more interesting options:
Remember that SimpleDateFormat is not thread safe and in general not recommended. Sensible pre Java 8 options are
Apache FastDateFormat and
joda-time package.
Both have some problems but certainly by far better then SimpleDateFormat (Joda-time package is very popular).
In Java 8 a new date and time hanling was introduced with package java.time.format It takes time to adjust to it but it works wonderful and resolves many problems that existed in that area. See class DateTimeFormatter.
And finally, I once had to write a utility that can take a String in any format and attempt to parse it to Date if possible. I wrote an article that describes how I implemented that Utility. I wrote it in Java 8, but the idea could be implemented in any version. See Java 8 java.time package: parsing any string to date

Upvotes: 0

kamehl23
kamehl23

Reputation: 522

Remove the time part in your pattern:

 DateFormat dateFormatTripStartDate = new SimpleDateFormat("dd-MMM-yyyy");

Upvotes: 4

Related Questions