Convert Date String to UTC format

10,376

Mon Oct 15 13:52:00 EDT 2018

is the result of date.toString() - Date class.

The date is parsed correctly, just displayed differently. If you want to check that the parsing is correct you can check like this:

String expiryDateString = "2018-10-15T17:52:00Z";
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(expiryDateString);
assertEquals(expiryDateString, formatter.format(date));
Share:
10,376
Easy Coder
Author by

Easy Coder

Updated on June 05, 2022

Comments

  • Easy Coder
    Easy Coder about 2 years

    I have a Date String which I am getting back from an API. The date is in this format.

    2018-10-15T17:52:00Z

    Now, I want to convert this string to UTC date format. This is the code I use for it,

    private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    
    
     try {
            String expiryDateString = "2018-10-15T17:52:00Z";
            final SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT,Locale.US);
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            return formatter.parse(expiryDateString);
         } catch (final ParseException e) {
                return null;
         }
    

    This method is returning the date as

    Mon Oct 15 13:52:00 EDT 2018

    I cannot use Java 8 functions. is there any way to convert string to UTC date time without using Java 8 methods?