2

I'm trying to get Date for timezone in UTC format and depend on the user-selected birthplace and timezone save it in the database but after setting the timezone and try to retrieve the new value it's returning the old format, not in UTC

val calendar = Calendar.getInstance().apply {
        timeZone = TimeZone.getTimeZone("UTC")
    }
    println("Date: ${calendar.time}")

I/System.out: Date: Sat Feb 26 16:43:08 GMT+02:00 2022

1 Answer 1

2

(Note: This Answer uses Java syntax, as I have not learned Kotlin. You can translate easily.)

tl;dr

Never use Calendar, SimpleDateFormat, TimeZone, or the other legacy date-time classes.

Generate text using a formatter object with the java.time classes.

ZonedDateTime.now( 
    ZoneId.of( "Pacific/Auckland" ) 
)
.format(
    DateTimeFormatter
    .ofLocalizedDateTime( FormatStyle.LONG )
    .withLocale( Locale.GERMANY )
)

See this code run live at IdeOne.com.

  1. Februar 2022 um 10:30:06 NZDT

Retrieve a moment from a database column of a type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE as an OffsetDateTime object. Adjust to your desired time zone. Generate text using DateTimeFormatter.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
String output = zdt.format( myFormatter ) ;

Details

You are using terrible date-time classes that are now legacy, years ago supplanted by the modern java.time classes defined in JSR 310.

👉 Date-time objects do not have a “format”. Date-time objects hold date-time values, not text. We use formatters to parse text into date-time objects, and use formatters to generate text from date-time objects.

Get current moment as seen in UTC using Instant.

Instant instant = Instant.now();

For more flexible formatting, convert to an OffsetDateTime.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

And use OffsetDateTime class when accessing a database column of a type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

To see that same moment as viewed through the wall-clock time used by the people of a particular region, assign a time zone (ZoneId) to produce a ZonedDateTime.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Generate text in a format that is automatically localized.

Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale ) ;
String output = zdt.format( f ) ;

See this code run live at IdeOne.com. Notice how the date as well as the time-of-day differs when viewed through the wall-clock time of Japan.

instant = 2022-02-27T21:23:27.804471Z
odt = 2022-02-27T21:23:27.804471Z
zdt = 2022-02-28T06:23:27.804471+09:00[Asia/Tokyo]
output = lundi 28 février 2022 à 06 h 23 min 27 s heure normale du Japon

All of this has been covered many many times already on Stack Overflow. Search to learn more.


The java.time classes are built into Java 8 and later. Android 26+ comes bundled with an implementation. For earlier Android, access most of the functionality through the "API desugaring" feature of the latest tooling.

1
  • Amazing. I will print this answer and hang it in my living room!
    – groff07
    Commented Aug 29 at 22:55

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.