2

I have the following date string Tue Feb 04 2020 16:11:25 GMT+0200 (IST) where I'm trying to convert into date time using the following code:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d yyyy HH:mm:ss O (zzz)", Locale.ENGLISH);
LocalDate dateTime = LocalDate.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);

And I got the following exception:

Text Tue Feb 04 2020 16:11:25 GMT+0200 (IST) could not be parsed at index 28

I look at the following SO question Java string to date conversion and I see that

O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;

So why I got the exception?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
ArmoArmo
  • 105
  • 10
  • 1
    Why do you call a variable `dateTime` when the type is `LocalDate`, i.e. date-only? --- Why are you parsing a string with date, time, and zone offset as `LocalDate`, instead of `OffsetDateTime`? – Andreas Jul 02 '20 at 06:59

7 Answers7

2

One of the reasons is that you are trying to parse a datetime String (date, time, zone and offset) to an object (LocalDate) that only stores year, month and day, nothing more.

Use a suitable class, say ZonedDateTime and adjust the parsing pattern a little:

  • you can't use the localized offset O because in a DateTimeFormatter it doesn't support the formatting your String has, which is GMT+0200 and the formatter supports GMT+8; GMT+08:00; UTC-08:00; only (mind the colon). Use a combination of an escaped GMT plus a regular offset symbol x
  • you have a single d but a representation of days that will always have two digits, so you need to use dd
  • you have to escape the brackets the zone abbreviation is enclosed in and I think a single z is sufficient for such an abbreviation

Considering all these aspects, you could parse the String as follows:

public static void main(String[] args) {
    String parsePattern = "EEE MMM dd yyyy HH:mm:ss 'GMT'x '('z')'";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(parsePattern,
                                                                Locale.ENGLISH);
    ZonedDateTime zdt = ZonedDateTime.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);
    System.out.println(zdt);
}

which then outputs (using the default formatter for ZonedDateTime)

2020-02-04T16:11:25+02:00[Asia/Jerusalem]
deHaar
  • 17,687
  • 10
  • 38
  • 51
2

The following pattern works.

"E MMM d u H:m:s 'GMT'Z (z)"

You can replace Z with x or X for the same result.

You can spell it out, if you want, but it is not necessary.

"EEE MMM dd uuuu HH:mm:ss 'GMT'ZZZ (zzz)"

You should parse that input to an OffsetDateTime, since the input string includes a Date, a Time, and an Offset.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM d u H:m:s 'GMT'Z (z)", Locale.ENGLISH);
OffsetDateTime dateTime = OffsetDateTime.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);
System.out.println(dateTime);

Output

2020-02-04T16:11:25+02:00
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

The problem here is the GMT+0200 if you use: GMT+02 it works. But as already mentioned in the comments it is a little confusing that you use a variable called dateTime on something of the type LocalDate. So your result will be only the date 2020-02-04 because LocalDate can only save this kind of data.

Max
  • 84
  • 6
0

2 things - ZonedDateTime & missing ':' for 0

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss O (zzz)");
ZonedDateTime dateTime = ZonedDateTime.parse("Tue Feb 04 2020 16:11:25 GMT+02:00 (IST)", formatter);
PrasadU
  • 2,154
  • 1
  • 9
  • 10
0

This should work

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d yyyy HH:mm:ss 'GMT'Z (z)", Locale.ENGLISH);
        LocalDate dateTime = LocalDate.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);
Mahesh
  • 21
  • 2
0

Do not use a fixed text for the timezone:

Do not use a fixed text (e.g. 'GMT') for the timezone as mentioned in the existing answers because that approach may fail for other locales.

The recommended solution:

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

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Tue Feb 04 2020 16:11:25 GMT+0200 (IST)";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("E MMM d u H:m:s VVZ (z)", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
        System.out.println(zdt);
    }
}

Output:

2020-02-04T14:11:25Z[Atlantic/Reykjavik]

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-2

you wrote one d and you passed 04 you should write dd instead of d like the following

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss O (zzz)", Locale.ENGLISH);

LocalDate dateTime = LocalDate.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);