javadatetimezone

What time zone does Date.toString() display?


By default, what time zone does method java.util.Date.toString() display? Since a Java Date stores a given date in UTC and doesn't contain any explicit time zone, does Date.toString() simply display the default time zone for the host, or TimeZone.getDefault()?


Solution

  • tl;dr

    java.util.Date replaced by java.time.Instant. The Instant class is straight-forward, no time zone injected; always in UTC.

    Instant.now().toString()  
    

    2025-01-23-12:30.123456Z

    The Z on the end is a standard abbreviation for an offset of zero hours-minutes-seconds, +00:00.

    Avoid legacy date-time classes

    The Answer by oxbow_lakes is correct, with java.util.Date#toString dynamically applying the JVM’s current default time zone while generating text. This well-intentioned but confusing anti-feature is but one of many reasons to never use the legacy date-time classes.

    java.time

    Use only the modern java.time classes for your date-time handling.

    The java.time classes “tell the truth”, in that there is no monkey-business with applying a time-zone on-the-fly. When generating text, the java.time objects express their true contents.

    Instant

    To determine the current moment as seen with an offset-from-UTC of zero hours-minutes-seconds, use java.time.Instant. This class replaces java.util.Date.

    Instant instant = Instant.now() ;  // Capture curent moment in UTC.
    

    Instant resolves to nanoseconds, in contrast to mere milliseconds in Date.

    ZonedDateTime

    To get the current moment as seen in a particular time zone, use java.time.ZonedDateTime. This class replaces java.util.GregorianCalendar.

    ZoneId z = ZoneId.of( "America/Edmonton" ) ;
    ZonedDateTime zdt = ZonedDateTime.now( z ) ;
    

    LocalDate

    To get the current date, use java.time.LocalDate. This class replaces java.sql.Date.

    While LocalDate contains no time zone, a time zone is required to determine today’s date. For any given moment, the time and the date both vary around the globe by time zone.

    LocalDate today = LocalDate.now( z ) ;
    

    Generating text

    When generating text from LocalDate, you get only the year, the month, and the day. No time zone involved.

    ISO 8601

    Generate text in standard ISO 8601 format.

    String output = today.toString() ;
    

    Localized

    Generate text in localized format.

    Locale locale = Locale.of( "fr" , "CA" ) ;  // French language, Canada cultural norms.
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( locale ) ;
    String localized = today.format( formatter ) ;