5

when i convert my string object in mm/dd/yyyy format to Date it gives me

java.text.ParseException: Unparseable date: "09/17/2014"

i am trying to do it like this :

String date= "09/17/2014";
DateFormat df = new SimpleDateFormat();
Date journeyDate= (java.sql.Date) df.parse(date);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Subham Tripathi
  • 2,683
  • 6
  • 41
  • 70
  • I recommend you don’t use `SimpleDateFormat` and the two `Date` classes. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 04 '21 at 10:40

5 Answers5

14

There are several potential problems here:

  • You're not specifying a format
  • You're not specifying a locale
  • You're not specifying a time zone
  • You're trying to cast the return value (which will be a java.util.Date reference) to a java.sql.Date - that would fail

You want something like:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
df.setTimeZone(...); // Whatever time zone you want to use
Date journeyDate = new java.sql.Date(df.parse(text).getTime());
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2
DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
Date journeyDate = df.parse(date); // gives you java.util.Date

If you want java.sql.Date then

java.sql.Date sqlDate = new java.sql.Date(journeyDate.getTime());
SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

I have the following exception:

java.text.ParseException: Unparseable date

System.out.println("inside the XMLGregorianCalendar");

sb = (String) map.get(fieldname);
System.out.println("THis is XMLGReogoriaaaaan calendar"+ sb);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date journeyDate = new java.sql.Date(df.parse(sb).getTime());
System.out.println("this"+journeyDate);
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

You have mixed m and M.

mstands for minute and M for month.

Below is an example of a working format.

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String dateInString = "07/06/2013"; 
Date date = formatter.parse(dateInString);
System.out.println(formatter.format(date));
Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43
Hrushi
  • 309
  • 3
  • 6
0

java.time

I recommend that you use java.time, the modern Java date and time API, for you date work. You can directly pass a java.time.LocalDate to your SQL database via JDBC (since JDBC 4.2) or JPA (for example since Hibernate 5.0).

You need to specify which format to assume for parsing:

private static final DateTimeFormatter DATE_FORMATTER 
        = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);

The rest is straightforward:

    String date = "09/17/2014";
    LocalDate journeyDate = LocalDate.parse(date, DATE_FORMATTER);
    System.out.println(journeyDate);

Output:

2014-09-17

I assumed that you were trying to specify that you wanted a java.sql.Date because you needed it for use with an SQL database. But you don’t need no java.sql.Date for that. For how to insert a LocalDate into SQL, see the link at the bottom.

If you need a java.sql.Date for a legacy API that you cannot afford to upgrade to java.time right now, the conversion is straightforward too:

    java.sql.Date oldfashionedJavaSqlDate = java.sql.Date.valueOf(journeyDate);
    System.out.println(oldfashionedJavaSqlDate);

2014-09-17

What happened in your code?

Jon Skeet has already pointed out the issues with your code. But only out of curiosity, more precisely, what happened? The no-arg SimpleDateFormat constructor gives you a localized format for date and time in your default locale. For example for en-IN locale the format could be like 17/09/14, 12:00 AM. In this case your formatter would parse 09 as the day of month, 17 as the month and 2014 as the year and then complaint with the exception because the comma and time were not present in the string.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161