英文:
Cannot parse date containing GMT to Timestamp in Java (Unparseable date Issue)
问题
I cannot convert the date containing GMT to timestamp in Java
Here is the code shown below
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);
}
}
Here is the error shown below
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)
How can I fix it?
英文:
I cannot convert the date containing GMT to timestamp in Java
Here is the code shown below
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);
}
}
Here is the error shown below
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)
How can I fix it?
答案1
得分: 0
I can provide a translation of the code portion for you:
如果你的日期格式合理,你可以执行以下操作,尽管应该避免使用`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);
Is there anything else you would like to translate or any other assistance you need?
英文:
Were your date format rational, you could do the following, although Timestamp
should be avoid too, instead, allowing your db driver to map a column to one of the classes in java.time
. Inserting +ve or -ve, so that the system knows what the offset means, the following is possible:
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);
答案2
得分: 0
这是我的答案如下
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
英文:
Here is my answer shown below
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;
}
}
Here is the output shown below
Thu Jun 01 2023 00:00:00 GMT +0300 (GMT +0300)
2023-06-01 00:00:00.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论