-2

I receive the following date format

private String newDate = "Mon Apr 25 04:50:00 CET 2022"

How can I convert it into the following format in java 8

2022-04-25T04:50:00
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Karin
  • 7
  • 5
  • Do you really want to change the time zone but not the time itself? – deHaar Apr 28 '22 at 10:37
  • Why would it involve springboot? – Maurice Perry Apr 28 '22 at 10:57
  • @deHaar I want to change the time as well in the desired format – Karin Apr 28 '22 at 11:00
  • You just removed the `Z` from your desired output. That would not change the time but drop the zone / offset. What exactly are you trying to achieve? Show time only in a different format? – deHaar Apr 28 '22 at 11:06
  • Yes, just to change the format.(sorry for the confusion as I am not sure of the exact value of Z here) – Karin Apr 28 '22 at 11:09
  • Does this answer your question? [DateTimeParse Exception](https://stackoverflow.com/questions/62105691/datetimeparse-exception). And/or [this](https://stackoverflow.com/questions/65856109/how-to-convert-java-string-eee-mmm-dd-hhmmss-zzz-yyyydate-type-to-java-util)? – Ole V.V. Apr 28 '22 at 11:43
  • Beware when parsing three letter time zone abbreviations. `CET` may be uniquely defined, but most of the most common abbreviations are ambiguous, and when parsing one that is, you don’t know what you get. – Ole V.V. Apr 28 '22 at 11:49

2 Answers2

4

You can use Java 8's date/time API, and more precisely the DateTimeFormatter.

There's no pre-defined formatter that matches your initial input. The closest is DateTimeFormatter.RFC_1123_DATE_TIME, but it requires a comma after the day of the week.

To solve that, you can write your own pattern:

DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ROOT));
String input = "Mon Apr 25 04:50:00 CET 2022";
ZonedDateTime date = ZonedDateTime.parse(input, inputFormatter);

The best way to represent your initial date is a ZonedDateTime, since your date contains zone-related information.

For your output, you can use the DateTimeFormatter.ISO_LOCAL_DATE_TIME formatter. For example:

String output = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(date);
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • 2
    I suggest adding an English-speaking locale such as `Locale.ROOT` to your formatter. On my Java and default locale your code does not work without it. `DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT)`. – Ole V.V. Apr 28 '22 at 11:52
3

Almost the same answer is already given. The given input format looks like Date.toSTring(), especially as weekday and month seem to be English, but might be German too. Below with Locale.ENGLISH (as suggested by "CET") but Locale.GERMAN might be better.

    String newDate = "Mon Apr 25 04:50:00 CET 2022";
    DateTimeFormatter formatter =
        DateTimeFormatter.ofPattern("eee MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
    ZonedDateTime t = ZonedDateTime.parse(newDate, formatter);
    System.out.println(t);
    System.out.println(Date.from(t.toInstant()));
    System.out.println(t.format(DateTimeFormatter.ISO_DATE_TIME));
    System.out.println(t.toLocalDateTime().format(DateTimeFormatter.ISO_DATE_TIME));
    System.out.println(t.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime()
        .format(DateTimeFormatter.ISO_DATE_TIME));

The output variants are:

Mon Apr 25 04:50:00 CEST 2022
2022-04-25T04:50+02:00[Europe/Paris]
2022-04-25T04:50:00+02:00[Europe/Paris]
2022-04-25T04:50:00
2022-04-25T02:50:00

It is indeed best to start with a ZonedDateTime.

If you get input all over Europe, it would be best to use Universal Time Coordinated, Greenwich time. (But it often depends on the existing database data.)

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138