I am receiving DateTime as a String from a webservice. An example of this DateTime string is: "DateTime":"2021-06-06T04:54:41-04:00".
This 2021-06-06T04:54:41-04:00  more or less matches the ISO-8601 format, so I have used this pattern to parse it: yyyy-MM-dd'T'HH:mm:ssZ. However, the colon in the timezone part of the response DateTime is causing issues. 2021-06-06T04:54:41-04:00 is giving parse exception, but 2021-06-06T04:54:41-0400 is parsing fine.
Below code should explain it better:
public void stringToDate() {
        String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";  //ISO - 8601 Format
        TimeZone timeZoneEST = TimeZone.getTimeZone("US/Eastern");
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, new Locale("en", "US"));
        sdf.setLenient(false);
        sdf.setTimeZone(timeZoneEST);
        
        String timeFromWebService = "2021-06-06T04:54:41-04:00";
        try {
            Date parsedDate = sdf.parse(timeFromWebService); // not working because of colon in timezone part
            System.out.println(parsedDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        try {
            Thread.sleep(1000);  //sleep to avoid interleaving output from stacktrace (above) and syso (below)
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        
        String timeFromWebServiceModified = "2021-06-06T04:54:41-0400";  //removed colon from timezone part
        try {
            Date parsedDate = sdf.parse(timeFromWebServiceModified); // working because colon is removed in timezone part
            System.out.println(parsedDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
    }
I want to handle this parsing without modifying the response DateTime. Any suggestions on how I can parse the original DateTime. Any suggestion on what pattern to use will be very help full.
 
     
    