0

I try to convert String to Date format but I got an exception!

Here is my code:

SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
startDate = format.parse(startDateString);

it should convert "14-MAY-2004" to Date. Thanks.

palaѕн
  • 72,112
  • 17
  • 116
  • 136
Sahar
  • 3
  • 2
  • 2
    "it got an exception", can you be a bit more specific about that? – Stultuske May 13 '20 at 06:18
  • You have : `dd-MMM-yyyy` it should be `dd-MM-yyyy` for date. If you want to have it with name of the MONTH. You have to Use LocalDate and the format "dd-LLLL-yyyy' – Luxusproblem May 13 '20 at 06:37
  • 1
    *What* is the exception? – akuzminykh May 13 '20 at 06:40
  • @Luxusproblem nope. MM means a numeric representation of month. "MAY" is MMM – Stultuske May 13 '20 at 06:49
  • you want final date to be in dd-MMM-yyyy format? or your input string is in dd-MMM-yyyy format and want it to be converted to date of any format? – NKR May 13 '20 at 09:09
  • 1
    This may behave locale -specific; did you specify an English locale? See my answer in https://stackoverflow.com/questions/57615971/java-simpledateformat-throwing-parseexception-unparseable-date-on-windows-but-n/57616212#57616212 – Robert May 13 '20 at 13:33
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former 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. May 13 '20 at 18:38
  • @Stultuske java.text.ParseException: Unparseable date: "14-MAY-2004" – Sahar May 18 '20 at 05:44
  • @akuzminykh java.text.ParseException: Unparseable date: "14-MAY-2004" – Sahar May 18 '20 at 05:45
  • @NKR the String is in dd-MMM-yyyy (14-MAY-2004) format and i want to convert it to Date format. – Sahar May 18 '20 at 05:47

2 Answers2

1

java.time

I recommend that you use java.time, the modern Java date and time API, for your date work.

    DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("d-MMM-uuuu")
            .toFormatter(Locale.ENGLISH);

    String startDateString = "14-MAY-2004";
    LocalDate startDate = LocalDate.parse(startDateString, dateFormatter);

    System.out.println(startDate);

Output is:

2004-05-14

Only if you indispensably need a Date object for a legacy API not yet upgraded to java.time, convert:

    Instant startOfDay = startDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
    Date oldfashionedDate = Date.from(startOfDay);

    System.out.println(oldfashionedDate);

Output in my time zone:

Fri May 14 00:00:00 CEST 2004

What went wrong in your code?

It’s almost certainly a locale problem. You didn’t specify a locale and hence no language for the month name or abbreviation. Always do that when the date string includes text in some language. Your SimpleDateFormat was using the default formatting locale of your JVM, and if that was a non-English-speaking locale, parsing was deemed to fail with an exception as you mentioned.

Links

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

From the statment "it should convert 14-MAY-2004 to date" i assume your input string is 14-MAY-2004 and you want this string to be converted to Date

String js="14-May-2004";
        Date dt=new Date(js);
        LocalDateTime localDateTime=dt.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        localDateTime.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));//or in the same format use- "dd-MMM-yyyy"
NKR
  • 167
  • 9