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"
6
  • 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. Commented Jun 7, 2019 at 6: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 Commented Jun 7, 2019 at 6:04
  • @DawoodibnKareem you mean I have to parse my date string"Wed Dec 02 00:00:00 ICT 2015" to a Date? Commented Jun 7, 2019 at 6: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. Commented Jun 7, 2019 at 6: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. Commented Jun 7, 2019 at 6:36

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

you are right, I have an incorrect understanding about the usage of SimpleDateFormat
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));

4 Comments

I expect that the date will have "YYYY-mm-DD" format not "EEE MMM dd HH:mm:ss z yyyy" format.
@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
I extended the code sample, to further format it
While this works in some cases (not always), using Date and SimpleDateFormat is strongly discouraged.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.