SimpleDateFormat 不解析 IST 日期

SimpleDateFormat not parsing IST date

我正在尝试解析此日期:2021 年 7 月 5 日,星期一 23:19:58 IST[​​=13=]

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);

但我收到此错误:java.text.ParseException: Unparseable date: "Mon, 05 Jul 2021 23:19:58 IST"

当我从格式中省略小写字母 z 时,我没有得到异常,但日期不在正确的时区。我已尝试执行以下操作:

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

但日期仍然显示在未来,这是不正确的。我怎样才能正确解析这个日期?谢谢。

不要使用 Date 或 SimpleDateTime。使用 java.time 包中的 类。

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));

打印

Mon, 05 Jul 2021 23:19:58 GMT

编辑

仔细阅读 java.time 包后,我发现 ZoneId.SHORT_IDS 包含 IST=Asia/Kolkata。因此,如果执行以下操作:

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

它打印

Mon, 05 Jul 2021 23:19:58 IST