无法将包含 GMT 的日期解析为 Java 中的时间戳(无法解析的日期问题)

Cannot parse date containing GMT to Timestamp in Java (Unparseable date Issue)

提问人:S.N 提问时间:6/8/2023 最后编辑:S.N 更新时间:6/9/2023 访问量:299

问:

我无法在 Java 中将包含 GMT 的日期转换为时间戳

下面是如下所示的代码

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DataParser {

    public static void main(String[] args) throws ParseException {
        
         String startDate = "Mon May 01 2023 00:00:00 GMT 0300 (GMT 03:00)";
         
         SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT' X (zzzz)");
         SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

         Date parsedDate = inputFormat.parse(startDate);

         String formattedDate = outputFormat.format(parsedDate);

         Timestamp timestamp = Timestamp.valueOf(formattedDate);
        
         System.out.println(timestamp);
    }
}

这是如下所示的错误

Exception in thread "main" java.text.ParseException: Unparseable date: "Mon May 01 2023 00:00:00 GMT 0300 (GMT 03:00)"
    at java.base/java.text.DateFormat.parse(DateFormat.java:399)
    at example1.DataParser.main(DataParser.java:17)

我该如何解决?

Java 日期 解析 时间戳 simpledateformat

评论

2赞 g00se 6/8/2023
您正在使用被取代的时间类。您应该使用 .你打算用 - 在数据库操作中使用它做什么?甚至,按原样,创建输入日期的新形式是多余的,以便创建何时可以使用现有日期的 a 来初始化它java.timeTimestampStringTimestamplongDate
0赞 S.N 6/8/2023
@g00se 首先,我尝试解析它,但我仍然遇到解析问题。我该如何解决?
0赞 g00se 6/8/2023
你能显示样本输入日期吗?
0赞 S.N 6/8/2023
@g00se 这里是Mon May 01 2023 00:00:00 GMT 0300 (GMT 03:00)
0赞 g00se 6/8/2023
这是一种棘手的格式。首先,您如何知道区域偏移是正的还是负的?

答:

0赞 g00se 6/8/2023 #1

如果您的日期格式合理,您可以执行以下操作,尽管也应该避免,而是允许您的数据库驱动程序将列映射到 中的某个类。插入 +ve 或 -ve,以便系统知道偏移量的含义,可以执行以下操作:Timestampjava.time

String d = "Mon May 01 2023 00:00:00 GMT +0300 (GMT +0300)";
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss z x '('z x')'", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse(d, inputFormat);
Timestamp ts = new Timestamp(OffsetDateTime.parse(d, inputFormat).toInstant().toEpochMilli());
System.out.println(ts);

评论

0赞 S.N 6/8/2023
我运行了代码,但我遇到了这个问题Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon May 01 2023 00:00:00 GMT +0300 (GMT +0300)' could not be parsed at index 0 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2106) at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:2008) at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:404) at example1.DataParser.main(DataParser.java:14)
0赞 deHaar 6/8/2023
将 a 传递给 ,其中包含与语言环境相关的信息。并非每个地区的星期一都是星期一Locale.ENGLISHDateTimeFormatterString
1赞 g00se 6/8/2023
是的@deHaar,我应该把它放进去
0赞 deHaar 6/8/2023
你仍然可以这样做,只需编辑你的答案......
0赞 S.N 6/8/2023
@g00se 我的字符串不像Mon May 01 2023 00:00:00 GMT 0300 (GMT 03:00)Mon May 01 2023 00:00:00 GMT +0300 (GMT +0300)
-1赞 S.N 6/9/2023 #2

这是我的答案,如下所示

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

public class DataParser {


    public static void main(String[] args) {
        String input = "Thu Jun 01 2023 00:00:00 GMT 0300 (GMT 03:00)";
        String output = manipulateString(input);
        System.out.println(output);
        
        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss z x '('z x')'", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse(output, inputFormat);
        Timestamp ts = new Timestamp(odt.toInstant().toEpochMilli());
        System.out.println(ts);
    }

    public static String manipulateString(String input) {
        String[] parts = input.split(" ");
        String gmtOffset = parts[parts.length - 3];
        String modifiedOffset = "+" + gmtOffset.substring(0, 2) + gmtOffset.substring(2);
        String modifiedInput = input.replace(gmtOffset, modifiedOffset);
        modifiedInput = modifiedInput.replaceFirst("\\(GMT\\s*", "(GMT +");
        modifiedInput = modifiedInput.replaceFirst(":\\d{2}\\)$", ":00)");
        
        int lastIndex = modifiedInput.lastIndexOf(":");
        if (lastIndex != -1) {
            modifiedInput = modifiedInput.substring(0, lastIndex) + modifiedInput.substring(lastIndex + 1);
        }

        return modifiedInput;
        
    }
    
}

下面是如下所示的输出

Thu Jun 01 2023 00:00:00 GMT +0300 (GMT +0300)
2023-06-01 00:00:00.0