-1

I'm having the date in this pattern EEEEE MMMMM yyyy HH:mm:ss.SSSZ, I would like to convert this to yyyy-MM-dd format in java, I tried below approach but I'm getting this exception java.text.ParseException: Unparseable date:

   SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    String dateInString = "Sun Oct 01 00:00:00 EDT 2017";

    try {

        Date date = formatter.parse(dateInString);
        System.out.println(date);
        System.out.println(formatter.format(date));

    } catch (ParseException e) {
        e.printStackTrace();
    }

Can someone please help me to resolve this?

Thanks.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
learn groovy
  • 487
  • 4
  • 11
  • 28
  • By `EEEEE` you mean `DDDDD`, right? – FailingCoder Oct 09 '19 at 22:56
  • Parsing and formatting at two seperate steps and you will need a formatter that represents each pattern to make it work – MadProgrammer Oct 09 '19 at 23:02
  • 2
    FYI, the troublesome date-time classes such as `java.util.Date`, `java.util.Calendar`, & `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) classes. Most *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Oct 09 '19 at 23:15
  • First of all 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 `ZonedDateTime` (or `OffsetDateTime`) and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 10 '19 at 04:50
  • `Sun Oct 01 00:00:00 EDT 2017` is *not* in pattern `EEEEE MMMMM yyyy HH:mm:ss.SSSZ`. It’s not in pattern `dd-MMM-yyyy` either and therefore cannot be parsed using a formatter with the latter pattern. – Ole V.V. Oct 10 '19 at 04:53

4 Answers4

4

From java-8 you can use the ZonedDateTime with pattern of your input date which is EEE MMM dd HH:mm:ss zzz yyyy

String dateInString = "Sun Oct 01 00:00:00 EDT 2017";

ZonedDateTime time = ZonedDateTime.parse(dateInString,DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(time.toLocalDate());   //2017-10-01

By default LocalDate is without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

You've defined your formatter as the concept 'date, month, year', and then you try to ask it to parse a string that isn't in this format at all. You need to make a formatter that can format Sun Oct 01 00:00:00 EDT 2017, and dd-MMM-yyyy obviously isn't it. The javadoc of SimpleDateFormat will tell you what combination of letters you need to use.

Once you've got that, it's easy: parse with this new formatter, then call .format with your old one (the dd-MMM-yyyy one).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

You can't use the same formatter for both parsing and formatting. See this answer: https://stackoverflow.com/a/999191

James
  • 11
  • 2
0

You double-create the DateFormat one parse and once to format

DateFormat dfParse = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
DateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd");
dfFormat.format(dfParse.parse("Sun Oct 01 00:00:00 EDT 2017"))
Jee Mok
  • 6,157
  • 8
  • 47
  • 80