提问人:S.N 提问时间:6/8/2023 最后编辑:S.N 更新时间:6/9/2023 访问量:299
无法将包含 GMT 的日期解析为 Java 中的时间戳(无法解析的日期问题)
Cannot parse date containing GMT to Timestamp in Java (Unparseable date Issue)
问:
我无法在 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)
我该如何解决?
答:
0赞
g00se
6/8/2023
#1
如果您的日期格式合理,您可以执行以下操作,尽管也应该避免,而是允许您的数据库驱动程序将列映射到 中的某个类。插入 +ve 或 -ve,以便系统知道偏移量的含义,可以执行以下操作:Timestamp
java.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.ENGLISH
DateTimeFormatter
String
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
评论
java.time
Timestamp
String
Timestamp
long
Date
Mon May 01 2023 00:00:00 GMT 0300 (GMT 03:00)