Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 733
user2532344 Avatar asked Jan 23 '26 23:01

user2532344


2 Answers

Remove the time part in your pattern:

 DateFormat dateFormatTripStartDate = new SimpleDateFormat("dd-MMM-yyyy");
like image 200
kamehl23 Avatar answered Jan 26 '26 14:01

kamehl23


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?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

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.

like image 38
Basil Bourque Avatar answered Jan 26 '26 14:01

Basil Bourque