javatimezonelocaldatezoneid

Java Timezone issue with LocalDate not showing correct date


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.


Solution

  • 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