-2

I'm currently working on a project that requires me to transform a date from one format to another.

Here is my code

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class testDate {

    static String date = "Wed Mar 27 18:14:25 CET 2019";                    // String representing received date
    static final String originalFormatS = "EEE MMM dd HH:mm:ss zzz yyyy";    // Corresponding date format
    static final String displayFormatS = "dd/MM/yy";                         // Wanted output format

    public static void main (String[] args) throws Exception {
        DateFormat originalFormat = new SimpleDateFormat(originalFormatS);
        DateFormat displayFormat = new SimpleDateFormat(displayFormatS);

        Date parsedDate = originalFormat.parse(date);
        String outputDate = displayFormat.format(parsedDate);

        System.out.println(outputDate);
    }

}

But then I get java.text.ParseException: Unparseable date: "Wed Mar 27 18:14:25 CET 2019"

I tried new Date(date); but it says it's deprecated...

Any idea about that ? Thanks in advance

  • 1
    Your code looks fine and should work correctly! – Safeer Ansari Apr 04 '19 at 12:29
  • 3
    You're better off using java.time instead of the deprecated java.util.Date API. – Ben R. Apr 04 '19 at 12:30
  • 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. Apr 04 '19 at 13:20
  • Always search Stack Overflow before posting. – Basil Bourque Apr 04 '19 at 14:30

3 Answers3

1

You're not in UK or US, are you?

SimpleDateFormat needs a locale to interpret the day/month names correctly.

Just create your SimpleDateFormat like that:

DateFormat originalFormat = new SimpleDateFormat(originalFormatS, Locale.US);

Works for me...

PS: like Ben R. said: consider using java.time for any date/time based data handling.

SirFartALot
  • 1,215
  • 5
  • 25
1

Please specify your Locale as you create an instance of SimpleDateFormat. For example making the constructor as below should work

DateFormat originalFormat = new SimpleDateFormat(originalFormatS,Locale.US);
muraguri2005
  • 119
  • 5
-1

Can you try this one?

public class testDate {

static String date = "Wed Mar 27 18:14:25 CET 2019";                    // String representing received date
static final String originalFormatS = "EEE MMM dd HH:mm:ss zzz yyyy";    // Corresponding date format
static final String displayFormatS = "dd/MM/yy";                         // Wanted output format

public static void main (String[] args) throws Exception {
    Date originalFormat = new SimpleDateFormat(originalFormatS).parse(date);
    DateFormat displayFormat = new SimpleDateFormat(displayFormatS);

    String outputDate = displayFormat.format(originalFormat);

    System.out.println(outputDate);
}

}