0

Here my java code that try to get current date in UTC on Android device:

public static Date getCurrentDateUTC() {
    try {
        TimeZone timeZoneUTC = TimeZone.getTimeZone("UTC");
        Date localTime = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
        dateFormat.setTimeZone(timeZoneUTC);
        String dateUTCAsString = dateFormat.format(localTime);
        Debug.d(TAG, "getCurrentDateUTC: dateUTCAsString = " + dateUTCAsString);
        Date dateResult = dateFormat.parse(dateUTCAsString);
        Debug.d(TAG, "getCurrentDateUTC: dateResult = " + dateResult);
        return dateResult;
    } catch (ParseException e) {
        Debug.e(TAG, "getCurrentDateUTC: ", e);
        return null;
    }
}

Result:

dateUTCAsString = 2017-11-15T12:54:25 +0000
dateResult = Wed Nov 15 14:54:25 EET 2017

As you can see the dateUTCAsString IS CORRECT show current date in UTC, but after parse the dateResult is not correct. Why?

9
  • Looks correct to me given that EET is UTC+2. What do you expect to see?
    – laalto
    Nov 15, 2017 at 12:58
  • The date must be in UTC. So I expect to see: Wed Nov 15 12:54:25 UTC 2017
    – Alex
    Nov 15, 2017 at 13:00
  • 1
    Date does not have any specific timezone. Date#toString() outputs in your local timezone which is EET.
    – laalto
    Nov 15, 2017 at 13:01
  • As result localTime and dateResult are equals. But it's incorrect. They have different of 2 hours between them.
    – Alex
    Nov 15, 2017 at 13:09
  • 1
    6 hours for sure. Nov 15, 2017 at 13:41

1 Answer 1

1

Excuse me for mentioning it, I suspect that there is no problem in your code, there’s only confusion. If you think the old Date class is behaving confusingly, allow me to be the first of many to agree with you. The good and sound solution to this problem is you stop using Date and start using the modern Java date and time API instead.

Since you are coding for Android, you first step is to get the ThreeTenABP, the library for Android that offers the modern API (if you were using Java 8 or 9, you could skip this step since the modern API wold be built in). The details are described in this question: How to use ThreeTenABP in Android Project. Now you can do:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss Z");
    String dateUTCAsString = "2017-11-15T12:54:25 +0000";
    Instant dateResult = OffsetDateTime.parse(dateUTCAsString, formatter).toInstant();
    System.out.println(dateResult);

On my computer this just printed:

2017-11-15T12:54:25Z

The Z at the end means Zulu time zone or UTC.

As you may know, System.out.println(dateResult) implicitly calls the toString method of the dateResult object. Objects of the class Instant produce the above format, always in UTC, as I understood you wanted. The Instant class is the natural replacement for the old-fashioned Date class for most purposes. Internally the Instant holds the number of seconds and nanoseconds since the epoch, which is defined as January 1 1970 at 0:00 midnight UTC. I encourage you to consider this an irrelevant implementation detail. An Instant is a point on the time-line.

What went wrong?

You asked for a date in UTC. Depending on how you look at it, you can or cannot have this.

  • On one hand a Date is implemented as the number of seconds and milliseconds since the epoch, so if you use the above definition of the epoch, you may say that it is always in UTC.
  • On the other hand you shouldn’t worry about implementation details. Conceptually a Date (like an Instant) is a point on the time-line, and does not and cannot have a time zone or offset; it cannot be in UTC. To make matters more confusing, when you do "getCurrentDateUTC: dateResult = " + dateResult, then dateResult.toString() is implicitly called. This method grabs your JVM’s time zone setting and converts the date-time to this zone and uses it for the generated string (without modifying the Date object). This is why you will see the time in EET on your computer or device no matter which Date you try to print.

java.time or JSR-310

The modern date and time API is know as java.time or JSR-310. One good source for learning to use it is the Oracle tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.