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));
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.timeDateclass. 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.java.util.Dateclass (and friends) were replaced by classes of thejava.timepackage (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 itstoString()method is being used [only recommended for debugging and so)