0

I have a date string "Wed Dec 02 00:00:00 ICT 2015". I tried to use SimpleDateFormat to convert this string to a date with format "YYYY-mm-DD". The code looks like this:

Date date = new SimpleDateFormat("YYYY-mm-DD").parse("Wed Dec 02 00:00:00 ICT 2015");

But I got an exception:

java.text.ParseException: Unparseable date: "Wed Dec 02 00:00:00 ICT 2015"
Men X
  • 153
  • 1
  • 2
  • 8
  • 1
    No, you have to `parse` your `String` using the format that it's in; then `format` your `Date` using the format that you want it to be in. Also better to use the Java 8 classes than the pre-Java 8 ones. – Dawood ibn Kareem Jun 07 '19 at 06:04
  • How does `Wed Dec 02 00:00:00 ICT 2015` match `YYYY-mm-DD`? The pattern and the input need to match before it will have any chance of parsing it. Having said that, you should be using the newer `java.time` API instead – MadProgrammer Jun 07 '19 at 06:04
  • @DawoodibnKareem you mean I have to parse my date string"Wed Dec 02 00:00:00 ICT 2015" to a Date? – Men X Jun 07 '19 at 06:07
  • 1
    Yes. The `parse` method converts a String to a Date. The `format` method converts a Date to a String. You'll need both. – Dawood ibn Kareem Jun 07 '19 at 06:16
  • 3
    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` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 07 '19 at 06:36
  • Also if there’s a way you can, don’t rely on parsing `ICT` as a time zone. While this particular abbreviation *may* only mean Indochina Time, the most commonly used three to five letter abbreviations are ambiguous, so the risk of getting an incorrect time is great. – Ole V.V. Jun 07 '19 at 06:42

2 Answers2

3

Most programming languages have a concept of "date/time" representation, which represents some point in time and allows the application of rules, such as time zones and leap years/seconds and other oddities to be applied.

When parsing a String value, you must know the format that the String is in, let's face it, what does 3/3/3 mean?

Java 8 replaced the existing Date/Calendar API which a much richer and less error prone API and you should make use of it as much as possible.

The first step is to construct a DateTimeFormatter of which represents the desired input format

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);

See the JavaDocs for more details on the specifiers.

Next, you want to parse the text using the formatter...

String text = "Wed Dec 02 00:00:00 ICT 2015";
ZonedDateTime zdt = ZonedDateTime.parse(text, formatter);
System.out.println(zdt);

nb: I've used a ZonedDateTime because I want to carry over the time zone information, you could use a LocalDateTime, but that would depend on your underlying needs

This will print 2015-12-02T00:00+07:00[Asia/Bangkok]

I expect that the date will have "YYYY-mm-DD" format

An important concept to get your head around is, the toString value of the a date object has nothing to do with its underlying representation and only represents a human readable representation of the object.

To format the date value into something else, you need to use another DateTimeFormatter...

String formattedDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(zdt);
System.out.println(formattedDate);

And the will print 2015-12-02

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Does this serves your purpose:

Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse("Wed Dec 02 00:00:00 ICT 2015");

//convert this date to the desired format
 DateFormat target = new SimpleDateFormat("yyyy-MM-dd");
 System.out.println(target.format(date));
TechFree
  • 2,600
  • 1
  • 17
  • 18
  • I expect that the date will have "YYYY-mm-DD" format not "EEE MMM dd HH:mm:ss z yyyy" format. – Men X Jun 07 '19 at 06:16
  • 1
    @MenX That's not how parsing works - it returns an instance of `Date`, which a representation of a the number of milliseconds since the unix epoch. You will need to use a different formatter to format the `Date` instance into a `String` of the desired format – MadProgrammer Jun 07 '19 at 06:19
  • I extended the code sample, to further format it – TechFree Jun 07 '19 at 06:20
  • While this works in some cases (not always), using `Date` and `SimpleDateFormat` is strongly discouraged. – Ole V.V. Jun 07 '19 at 06:38