I am trying to convert a GMT time zone from other server to EST considering day light saving to display the correct date but not able to do.
The GMT time format is coming in json String as "yyyy-MM-dd 'T' HH:mm:ss 'Z'". To this format we are setting time zone as EST but not getting correct result.
Example - date coming as "2020-06-02T03:53:57Z" , while the correct date in EST when created was "2020-06-01T11:53:57Z".
1 Answer
To convert between two explicit time zones, specify those time zones on the SimpleDateFormat objects used to parse and re-format the date string.
String input = "2020-06-02T03:53:57Z"; SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); inputFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = inputFormat.parse(input); SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); outputFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); System.out.println(outputFormat.format(date)); Output
2020-06-01 23:53:57 EDT As you can see, in June, the time zone is EDT, not EST, and it is certainly not Z, aka UTC.