Skip to content
Advertisement

How to convert Date string into UTC Date object?

I’m learning Java and come across this issue. I have a date string with the given format.

String dbTime = "01/01/1998 12:30:00";
final String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";

Now I wanted to initialize/create a Date object of UTC timezone. For this, I have tried below code

    SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT);
    TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
    sdfAmerica.setTimeZone(utcTimeZone);

    String sDateInAmerica = sdfAmerica.format(date); // Convert to String first
    Date dateInAmerica = new Date();
    try {
        dateInAmerica = formatter.parse(sDateInAmerica); // Create a new Date object
    } catch (ParseException e) {
        e.printStackTrace();
    }

This will convert the time into UTC instead of just creating a date object.

01/02/1998 23:00:00

Now I’m confused as to which is the correct approach to convert the time. I have time in string format and I have to convert it into different formats mainly UTC to PST or PST to UTC.

After some research, I found this tutorial but was unable to get the expected output.

Advertisement

Answer

The java.util.Date class is not optimal to start with. While it looks like a full date from the outside, it actually only represents a timestamp without storing actual timezone information.

On Java 8 and later I’d suggest to stick with the better designed java.time.* classes.

    String dbTime = "01/01/1998 12:30:00";
    String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";

    // parsed date time without timezone information
    LocalDateTime localDateTime = LocalDateTime.parse(dbTime, DateTimeFormatter.ofPattern(DATE_FORMAT));

    // local date time at your system's default time zone
    ZonedDateTime systemZoneDateTime = localDateTime.atZone(ZoneId.systemDefault());

    // value converted to other timezone while keeping the point in time
    ZonedDateTime utcDateTime = systemZoneDateTime.withZoneSameInstant(ZoneId.of("UTC"));

    // timestamp of the original value represented in UTC
    Instant utcTimestamp = systemZoneDateTime.toInstant();


    System.out.println(utcDateTime);
    System.out.println(utcTimestamp);

As you can see from the names alone there are different classes for different use-cases of dates.

java.time.LocalDateTime for example only represents a date and time without a specific timezone context and therefore can be used to parse your string value directly.

To convert timezones, you first have to convert into the a ZonedDateTime, which accepts date, time and timezone. I’ve intialized the sample on “systemDefault”, as on most smaller apps you can use the JVM and OS’es default value to assume the current timezone. You could also use ZoneId.of(“America/Los_Angeles”) directly if you want to make sure the value is interpreted as pacific time.

This value can be converted into another ZonedDateTime in another timezone, e.g. UTC.

For UTC especially you could also use the Instant class, which represents only a UTC timestamp and can also be used as a basis for most other types

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement