Dragon

Aug 14, 2019 - Timestamp And Localdatetime

Comments

When I used LocalTimeDate and Timestamp, I got stuck some problem convert LocalTimeDate to Timestamp.

Long story short

LocalTimeDate.MIN and LocalTimeDate.MAX should NOT be converted to Timestamp. It returns an unexpected result.

What I did

Timestamp.valueOf(LocalDateTime.MIN);
Timestamp.valueOf(LocalDateTime.MAX);

I expected that I could get max and min values of Timestamp, but…

Result

Converted MIN to Timestamp : 169087565-03-15 04:51:43.0
Converted MAX to Timestamp : 169104628-12-10 19:08:15.999999999

So it does not give you what you want. * I assume that you want the max and min values of Timestamp.

I found later this below from Java document.

The precision of a Timestamp object is calculated to be either:
19 , which is the number of characters in yyyy-mm-dd hh:mm:ss
20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision.

Source: Java document

It seems that LocalDateTime.MIN and MAX are outside of the scope.

Lessons I leaned

  • Don’t be too lazy. I should have created just another method. I tried to reuse method method(Timestamp start, Timestamp end).
  • I should check the result when you convert class to another class.

Categories: Java

Aug 14, 2019 - Timezone

Comments

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

Jul 30, 2019 - First Post

Comments

Hello everyone! This is my first post on this page.

This page will be my memos and tips that those might be helpfull for you as well.

My plans are below.

  • Keep updating what I lean.
    • Java
    • Python
    • Unix like OSs
    • etc… (git or some tools)
  • Maybe post in Japanese sometimes.

Categories: general