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

449 Views Asked by At

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?

2

There are 2 best solutions below

0
S.N On BEST ANSWER

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
0
g00se On

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);