Timezone

Timezone is a regeion of the globe that observes a uniform standard time for legal, commercial, and social puposes. from wikipedia

What is UTC

UTC(Cordinated Universal Time) is standard time and more precice than GMT(Greenwich Mean Time) because it uses leap second. UTC +- 0 places are GMT +- 0(GST: Greenwich Standard Time).

Some places do not have three character code like PST, EDT.

Handle timezone with Java

Java has TimeZone class. You can get timezone depends on where the device is.

TimeZone tz = Timezone.getDefault();

This creates a TimeZone babsed on the time zone where the program is running.

You can also get other places timezone.

Timezone tz = Timezone.getTimeZone("Asia/Tokyo");

Zoned Time

Java has ZonedDateTime class which can hold timezone information.

This is a date-time with a time-zone in the ISO-8601 calendar system.

System.out.println(ZonedDateTime.now());

result

2019-08-14T14:08:49.054-07:00[America/Los_Angeles]

If you want to get other timezone time.(Convert time to other location.)

zdt.withZoneSameInstant(TimeZone.getTimeZone("Asia/Tokyo").toZoneId());
//withZoneSameInstant(ZoneID)

Result

Local : 2019-08-14T14:18:24.177-07:00[America/Los_Angeles] -> Remote: 2019-08-15T06:18:24.177+09:00[Asia/Tokyo]

The time is converted to other location time. These two are the same time.

If you want to just change timezone in the data. In other words, keep the time and change only timezone.(Convert timezone information to other location.)

zdt.withZoneSameLocal(TimeZone.getTimeZone("Asia/Tokyo").toZoneId());
//WithZoneSameLocal(ZoneId)

Result

Local : 2019-08-14T14:17:28.705-07:00[America/Los_Angeles] -> Remote: 2019-08-14T14:17:28.705+09:00[Asia/Tokyo]

This is different time. Since the time is the same but timezone is different.

Convert to UTC

I think convert to UTC and UTC to local time is usuful because mySQL TIMESTAMP does not hold timezone information.

Timestamp Local to UTC

Timestamp tsLocal = Timestamp.valueOf(LocalDateTime.now());
ZonedDateTime zdt = ZonedDateTime.of(tsLocal.toLocalDateTime(), TimeZone.getDefault().toZoneId());
Timestamp tsUTC = Timestamp.valueOf(
	zdt.withZoneSameInstant(
		TimeZone.getTimeZone("UTC").toZoneId()).toLocalDateTime());

1: To create Timestamp using valueOf and LocalDateTime.now(). If you retrieve a Timestamp from your application, you do not need to create this. 2: To add local timezone information to current date and time. Get and add computer’s defualt timezone. 3: Converting Local date and time to UTC date and time.

//Timestamp.valuOf(LocalDateTime)

Timestamp.valueOf takes LocalDateTime, so you need to convert ZonedDateTime to LocalDateTime using toLocalDateTime().

Timestamp UTC to Local

Timestamp tsUTC = Timestamp.valueOf(LocalDateTime.now());
ZonedDateTime zdt = ZonedDateTime.of(tsUTC.toLocalDateTime(), TimeZone.getTimeZone("UTC").toZoneId());
Timestamp tsLocal = Timestamp.valueOf(
	zdt.withZoneSameInstant(
		TimeZone.getDefault().toZoneId()).toLocalDateTime());

This is basically just vice versa of Local to UTC.

1: To create Timestamp using valueOf and LocalDateTime.no(). If you retrieve a Timestamp from database, you do not need to create this. 2: To add UTC timezone information to the date and time. 3: Converting UTC date and time to Local date and time.

categories: Java