0

I have a date pass from Angular frontend to Java backend.

The date format I received from frontend is: Wed Mon 26 11:11:59 SGT 2022 and is in Java Date object

How can I convert this format to dd/MM/yyyy and the final output should be 26/12/2022 in Java Date format.

Currently my code is like this:

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                                        Locale.ENGLISH);

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

Date formattedDate = dateFormat.parse(sdf.format(dateFromFrontend));

===> formattedDate to save to DB

Parse Exception that I get:

java.text.ParseException: Unparseable date: "Mon Dec 26 12:43:19 SGT 2022"
Foody
  • 177
  • 1
  • 10
  • Looks like a typo, you used `dateFormat.parse` and `sdf.format` when, by your description, you wanted to parse with `sdf` and format with `dateFormat` instead. Of course, if `dateFromFrontend` is already a `Date` object, you don't need to worry about parsing it at all. A formatted `Date` is a `String`. – Dan Getz Dec 02 '22 at 05:07
  • @DanGetz, I have update the post. What I want is that I want the final date in Java Date and of format (26/12/2022). The dateFromFrontend is already a Java date, but after I format it, it will become a string. Then I try to parse back as Date but when I run, I get the ParseException. Is there anything wrong in this process? – Foody Dec 02 '22 at 05:33
  • I see no reason to format it as a `String` and then re-parse it again. If you want to format it, just format it once with the format you want to use. Does `dateFormat.format(dateFromFrontend)` not give you the string you want? (If it doesn't, please [edit] to explain what you're actually trying to do.) – Dan Getz Dec 02 '22 at 05:48
  • I strongly 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. Dec 02 '22 at 20:21
  • Also don’t format your date before saving it in the database. Depending on your SQL type and your JDBC driver pass a `LocalDate`, an `OffsetDateTime`, `Instant` or `LocalDateTime` to JDBC and don’t worry about the format. – Ole V.V. Dec 02 '22 at 20:24
  • *How can I convert this format to dd/MM/yyyy and the final output should be 26/12/2022 in **Java Date format**.* If I understand correctly what you mean, that is neither possible nor desirable. A `Date` object cannot have a format. – Ole V.V. Dec 02 '22 at 20:28
  • What your current code does: It formats your `Date` to a string, `"Mon Dec 26 12:43:19 SGT 2022"` (same format that you would get from simply calling its `toString` method). It then tries to parse this string back into a `Date` (without format, as I said) as though the string had format `dd/MM/yyyy`. Since your string has not got that format, parsing fails with the exception you saw. – Ole V.V. Dec 02 '22 at 20:31

2 Answers2

3

First, you need to convert String to LocalDate. Then you apply your required format.

import java.util.Locale;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Test {

    public static void main(String[] args) {
        String raw = "Mon Dec 26 11:11:59 SGT 2022";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);

        // Your required format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

        LocalDate dateTime = LocalDate.parse(raw, dtf);

        System.out.println(formatter.format(dateTime));
    }
}

Output:

26/12/2022
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
TANIMUL ISLAM
  • 320
  • 2
  • 5
1

If you are using at least Java 8, then you should use the date-time API. The documentation for class java.time.format.DateTimeFormatter details the recognized pattern characters. Recommended to also indicate the Locale.

By the way, 26th December, 2022 is a Monday and not a Wednesday. In the below code, using the example from your question, I get the following error message:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Wed Dec 26 11:11:59 SGT 2022' could not be parsed: Conflict found: Field DayOfWeek 1 differs from DayOfWeek 3 derived from 2022-12-26

Since the example in your question contains a time zone, the below code parses the string to a ZonedDateTime but you could also parse it to a different class, depending on your requirements – which aren't clear to me from your question.

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Doctor {

    public static void main(String[] args) {
        String raw = "Mon Dec 26 11:11:59 SGT 2022";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(raw, dtf);
        System.out.println(zdt);
    }
}

Running the above code gives following output:

2022-12-26T11:11:59+08:00[Asia/Singapore]

If you want to save the date in your database, then the data type of the column in the database table should be a [database] date type. Hence you need to use JDBC so the format is not important since you need to convert the ZonedDateTime to a java.sql.Date.

java.sql.Date d = java.sql.Date.valueOf(zdt.toLocalDate());

where zdt is the value obtained in the code, above.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Hi Abra, I think the Wed is typo, I already update the pots. However, I want to final format is in 26/12/2022 and is in Java Date. How can I convert to this format ya? – Foody Dec 02 '22 at 05:37