Skip to content Skip to sidebar Skip to footer

"eee Mmm Dd Hh:mm:ss Zzz Yyyy" Date Format To Java.sql.date

I am trying to convert EEE MMM dd HH:mm:ss ZZZ yyyy to YYYY-MM-DD format, so I can insert it into a MySQL database. I do not get any error, but the date inserted into my db is wron

Solution 1:

    LocalDate date4 = ZonedDateTime
            .parse(date, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH))
            .toLocalDate();
    java.sql.Date date5 = java.sql.Date.valueOf(date4);

I am using the modern classes in the java.time package. You notice that the code is not only simpler, once you get acquainted with the fluent writing style of the newer classes, it is also clearer.

If you wanted to be 100 % modern, you should also check out whether your latest MySQL JDBC driver wouldn’t accept a LocalDate directly without conversion to java.sql.Date. It should.

A few details to note

  • If you need your code to run on computers outside your control, always give locale to your formatter, or your date string cannot be parsed on a computer with a non-English-speaking locale. You may use Locale.ROOT for a locale neutral locale (it speaks English).
  • If you can, avoid the three letter time zone abbreviations. Many are ambiguous. EET is really only half a time zone since some places where it’s used are on EEST (summer time) now. Better to use either a long time zone ID like Europe/Bucharest or an offset from UTC like +02:00.

These points are valid no matter if you use DateTimeFormatter or SimpleDateFormat.

If you cannot or do not want to move on to the recommended newer classes, the fix to your code is:

SimpleDateFormatformatnow=newSimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); 
    SimpleDateFormatformatneeded=newSimpleDateFormat("yyyy-MM-dd");

I am using lowercase zzz since this is documented to match a three-letter time zone name, I know that uppercase ZZZ works too. I have added locale. And maybe most importantly, in the needed format I have changed YYYY (week-based year) to yyyy (calendar year) and DD (day of year) to dd (day of month). All those letters are in the documentation.

Solution 2:

I know this question is related to java.sql.Date but as additional information, if you want to convert the date into LocalDate then below code might help:

privateLocalDategetLocalDate(String date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss Z yyyy", Locale.getDefault());
        returnLocalDate.parse(date, formatter);
    }

And once you get the LocalDate, you can transform it to any format. As the question expects yyyy-MM-dd then just call toString() on LocalDate object.

    LocalDate curr = LocalDate.now();
    System.out.println(curr.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    System.out.println(curr.toString());

It will display the date like "2019-11-20".

Hope this helps to someone.

Post a Comment for ""eee Mmm Dd Hh:mm:ss Zzz Yyyy" Date Format To Java.sql.date"