-2

I am working on a Java 8 application where I receive epoch millisecond timestamps that represent UTC time. I need to pass these timestamps as Date objects to a method that I do not control (from a third-party library). That method eventually outputs the date but the string representation always reflects the system’s time zone (UTC-3), even though the timestamp was in UTC.

I cannot see or modify the implementation of that method. What I only know is: it ends up showing the wrong clock time (UTC-3) instead of the expected UTC time.

Is there any way to ensure that a Date object, when displayed by a system that I do not control, will be shown in UTC? Without changing the system default time zone?

Debugging details

Desired behviour: I want Date.toString() to return the UTC time, for example Sun Jun 15 15:06:40 UTC 2025.

Specific problem: Date.toString() returns time in the default time zone of the JVM, for example Sun Jun 15 12:06:40 GMT-03:00 2025.

Shortest code to reproduce:

System.out.println(new Date(1_750_000_000_000L));
19
  • 2
    Not that I'm aware of, depending on how it is printed. If it's just casually someDate.toString()-ed, one could try subclassing Date and ovverriding that method... However, it might just be time to figure out a way to move to java.time Commented Jul 17 at 12:15
  • 1
    It’s one of the many troubles with the bad old Date class. Epoch milliseconds are independent of time zone. I know that some say they are in UTC, I don’t really regard this as perfectly correct. Commented Jul 17 at 12:16
  • 2
    the java.util.Date class (and friends) were replaced by classes of the java.time package (more than 10 years ago) -- ("Why does Java Date always display in system time zone instead of UTC?" because it is the way it was implemented (assuming its toString() method is being used [only recommended for debugging and so) Commented Jul 17 at 12:21
  • 1
    Why are there 4 downvotes on this clear, valid and legitimate question? Commented Jul 18 at 6:29
  • 1
    I post it again, as this question has been reopened: this is a duplicate of How do I display a date with a custom timezone?, and otherwise the answer is "you can't". Commented Jul 18 at 7:30

2 Answers 2

1

As far as I can tell, the default implementation of Date does not offer any ability to configure this out-of-the-box.

However, if what the third-party code does is simply calling toString() on the Date instance, you can create your own class MyBetterDate extends Date subclass where you override the toString() method to present the value in whatever way you want.

Sign up to request clarification or add additional context in comments.

3 Comments

The way to configure it, is to use an appropriate formatter and set the time zone on that formatter.
Or (depending on taste) convert via Instant to either OffsetDateTime or ZonedDateTime in UTC and format the result using a formatter that doesn’t need itself to care about time zone.
There is no point is saying what would be better when the questioner already said that this is not possible because the method can not be changed. If the method can not be changed and it unconditionally invokes toString(), then changing the toString() method is the only option. The long-term solution might be switching away from that 3rd party library…
0

java.time

I know you said you are calling a third-party method out of your control. You should still see if there is some way of obtaining a newer version of that third-party method that supports java.time, the modern Java date and time API since Java 8. Specifically, the class java.time.Instant.

Instant instant = Instant.ofEpochMilli ( 1_750_000_000_000L ) ;
String output = instant.toString() ;  // Generates text in standard ISO 8601 format. 

See that code run at Ideone.com.

2025-06-15T15:06:40Z

Or consider if you can pressure the developers to develop such a new version. Or see if you can find a replacement for this obviously outdated software.

No, what you ask is not possible

While not perfectly well documented, Date always prints in the default time zone of the JVM. So yes, changing the default would give you what you ask for. And will influence all other parts of your program, all 3rd party libraries and all other programs running in the same JVM. Think at least twice.

The awful hack: Write your own subclass

As mentioned in the other answer, if you want the really awful hack, you can write a subclass and override the toString method:

class HackedUtcDate extends Date {
    public HackedUtcDate(long date) {
        super(date);
    }

    @Override
    public String toString() {
        return toInstant().toString();
    }
}

Put into the toString method whatever you like. You may for example apply a DateTimeFormatter with an override zone of UTC for formatting the Instant that you get from toInstant().

Try it out in isolation:

    Date myHackedUtcDate = new HackedUtcDate(1_777_000_000_000L);
    System.out.println(myHackedUtcDate);

Output:

2026-04-24T03:06:40Z

The trailing Z informs the reader that this is in UTC, per ISO 8601. As I said, change to your needs.

Whether this works depends on your third party method, so we cannot know until after you have tested thoroughly (and not even then be sure to have covered all corner cases). Perhaps your method does not accept this subclass and starts misbehaving. Perhaps the object that it prints is not the same object that your are giving it. You may try.

2 Comments

btw, if still using this almost deprecated class , return toGMTString() could be used instead of toInstant().toString() - it is deprecated but not yet marked for deletion (Java 25)
You can display a date with a different time zone by using a suitable formatter and set the time zone on that formatter.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.