Java Timezone issue with LocalDate not showing correct date

93 Views Asked by At

I'm currently based in Europe/London

I am trying to get the current day in Samoa, which is 27 July with the following code:

LocalDate localDate = LocalDate.now(ZoneId.of("Pacific/Samoa"));

However for some reason this outputs as:

2023-07-26

It's currently 16:47, 26 July in London, and 4:47, 27 July in Samoa.

I don't understand why I'm getting the 26 as output if Im using Samoas zoneId.

2

There are 2 best solutions below

1
On BEST ANSWER

The problem is that Pacific/Samoa is no longer the time zone for Samoa, but only the time zone for American Samoa. And in American Samoa, it is currently 05:07 on the 26th of July (18:07 on the 26th of July Europe/Amsterdam).

A few years ago (in 2011), Samoa transited the international dateline resulting in a new time zone, and you need to use the time zone Pacific/Apia:

jshell> LocalDate.now(ZoneId.of("Pacific/Apia"))
$3 ==> 2023-07-27
jshell> LocalDateTime.now(ZoneId.of("Pacific/Apia"))
$4 ==> 2023-07-27T05:07:08.188250
2
On

According to the docs, you'll want to use a ZoneId of Pacific/Pago_Pago to get Samoan time.

Edit: As Jon corrected below; the doc I've linked to is old and Pacific/Samoa is an acceptable ID. Leaving this here as reference.