0

I'm trying to parse this date: Mon, 05 Jul 2021 23:19:58 IST

String date = "Mon, 05 Jul 2021 23:19:58 IST";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.getDefault());
dateFormat.parse(date);

But I'm getting this error: java.text.ParseException: Unparseable date: "Mon, 05 Jul 2021 23:19:58 IST"

When I omit the lowercase z from the format, I don't get the exception, but the date isn't in the correct timezone. I've tried doing the following:

dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));

But the date still shows in the future, which is incorrect. How can I correctly parse this date? Thank you.

Josh
  • 3
  • 1
  • 1
    Your code works fine for me, so I guess this issue is related to your default locale. Anyway, SimpleDateFormat and Date are outdated and Date can't even handle timezones correctly anyway. This Q/A should provide more information: [SimpleDateFormat parse loses timezone](//stackoverflow.com/q/18122608) – Tom Jul 06 '21 at 01:08
  • Don't use `Locale.getDefault()`. Since the string has English words (`Mon` and `Jul`), specify an English locale, e.g. `Locale.ENGLISH`, `Locale.US`, `Locale.UK`, ... – Andreas Jul 06 '21 at 02:41
  • Which is your default locale? (`Locale.getDefault()`) – Ole V.V. Jul 06 '21 at 03:06
  • I too 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. Jul 06 '21 at 03:07

1 Answers1

1

Don't use Date or SimpleDateTime. Use the classes in the java.time package.

String date = "Mon, 05 Jul 2021 23:19:58 IST";

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(
        "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
ZonedDateTime zdt = ZonedDateTime.parse(date,dateFormat);
System.out.println(zdt.format(dateFormat));

prints

Mon, 05 Jul 2021 23:19:58 GMT

EDIT

After perusing the java.time package I found that ZoneId.SHORT_IDS contained IST=Asia/Kolkata. So if one does the following:

ZonedDateTime zdt = ZonedDateTime.parse(date,dateFormat)
              .withZoneSameLocal(ZoneId.of("Asia/Kolkata"));
System.out.println(zdt.format(dateFormat));

It prints

Mon, 05 Jul 2021 23:19:58 IST
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Thanks for showing the modern and recommended solution. I am not sure why `DateTimeFormatter` interprets IST as GMT. The asker may have had a different time zone in mind. For how to control the interpretation of such an ambiguous time zone abbreviation see [how convert date" formate including IST](https://stackoverflow.com/questions/55806397/how-convert-date-formate-including-ist). – Ole V.V. Jul 06 '21 at 03:15
  • @OleV.V. I found a way to force `IST` to appear. But it seems rather unorthodox. And I am not all that fluent on the subtleties of `java.time` – WJS Jul 06 '21 at 15:02
  • You’re right, it’s not really orthodox. I know it’s more complicated, still I prefer `DateTimeFormatter dateFormat = new DateTimeFormatterBuilder().appendPattern("EEE, dd MMM yyyy HH:mm:ss ").appendZoneText(TextStyle.SHORT, Collections.singleton(ZoneId.of("Asia/Kolkata"))).toFormatter(Locale.US)`, then you original code works fine. Obviously substitute whichever time zone was desired for IST instead of Asia/Kolkata, I don’t think we know whether it was that one. – Ole V.V. Jul 06 '21 at 15:12