将 "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)" 格式的日期转换为 Java 中的“2020-02-11 16:05:00”格式

To Convert Date in this format "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)" to this format "2020-02-11 16:05:00" in Java

我收到的电子邮件到达日期类似于“Tue,14 Jul 2020 03:15:03 +0000 (UTC)”,这需要转换成这种格式“2020-02-11 16:05:00”。谁能帮我实现这个日期转换?

部分形成的输入日期格式如: EEE, d MMM yyyy HH:mm:ss

谁能给我输入日期的确切日期格式?

我试过的:

try
     {
        String date_s = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
        SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
        Date tempDate=simpledateformat.parse(date_s);
        SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");           
        System.out.println("Output date is = "+outputDateFormat.format(tempDate));
      } catch (Exception ex) 
      {

          ex.printStackTrace();
      }

异常如下:

java.text.ParseException: Unparseable date: "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at JavaPackage.DateConvertion.main(DateConvertion.java:12)

等待您的回复。

注意: 仅供日期格式识别之用,上面随机给出转换后的日期。

java.util 的日期时间 API 及其格式 API、SimpleDateFormat 已过时且容易出错。我建议你应该完全停止使用它们并切换到 modern date-time API.

使用现代日期时间 API:

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

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("EEE,d MMM uuuu H: m: s Z '('z')'", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
        System.out.println(dtfOutput.format(odt));
    }
}

输出:

2020-07-14 03:15:03

Trail: Date Time.

了解有关现代日期时间 API 的更多信息

如果您正在为 Android 项目工作,并且您的 Android API 水平仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaring and

使用旧版 API:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        String strDateTime = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
        DateFormat sdfInput = new SimpleDateFormat("EEE,d MMM yyyy H: m: s Z '('z')'", Locale.ENGLISH);
        Date date = sdfInput.parse(strDateTime);

        DateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        sdfOutput.setTimeZone(sdfInput.getTimeZone());
        System.out.println(sdfOutput.format(date));
    }
}

输出:

2020-07-14 03:15:03

您的字符串中出现空格看起来很有趣。如果空格总是出现在这些地方,请使用 Arvind Kumar Avinash 的答案。如果空格的出现有变化,您可以通过在格式模式字符串中将空格括在方括号中来处理它们,因此:

    DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern(
            "EEE,[ ]d MMM uuuu H[ ]:[ ]mm[ ]:[ ]ss xx[ ]'('z')'", Locale.ENGLISH);

    String strDateTime = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
    OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);
    
    System.out.println(odt);

输出:

2020-07-14T03:15:03Z

格式模式字符串中的方括号包含可选部分,因此上面的格式化程序将解析包含或不包含这些空格的字符串。

Link

Oracle tutorial: Date Time 解释如何使用 java.time。