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

huangapple go评论80阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月8日 19:33:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定