0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    You're wrong, it is currently 26 July 2023 04:54 in Samoa, while it is 26 July 2023 16:54 in London. See https://www.timeanddate.com/worldclock/american-samoa. It has UTC -11, not UTC +11 – Mark Rotteveel Jul 26 '23 at 15:55
  • @MarkRotteveel Samoa and American Samoa are not the same country: https://www.timeanddate.com/worldclock/samoa – OH GOD SPIDERS Jul 26 '23 at 15:58
  • 2
    @OHGODSPIDERS Sure, but Pacific/Samoa refers to American Samoa these days (in the past it was also for Samoa). The one for Samoa is Pacific/Apia – Mark Rotteveel Jul 26 '23 at 16:03
  • @OHGODSPIDERS You're right, I focused too much on the time zone and not the country :) – Mark Rotteveel Jul 26 '23 at 16:12

2 Answers2

3

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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    As a quick sidenote one can always print the rules of the ZoneIds to check what their actual offset is. eG `ZoneId.of("Pacific/Samoa").getRules()` will show `currentStandardOffset=-11:00` while the ZoneId `ZoneId.of("Pacific/Apia").getRules()` will show `currentStandardOffset=+13:00`. Knowing the actual offset values is certainly a useful tool when debugging. – OH GOD SPIDERS Jul 26 '23 at 16:18
-1

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.

Loz
  • 118
  • 11
  • That looks like it's a pretty old table (2015 if https://docs.oracle.com/middleware/1221/wcs/tag-ref/index.html is to be believed). `Pacific/Samoa` is a perfectly good time zone ID. Mark Rotteveel's comment is a more appropriate response IMO. – Jon Skeet Jul 26 '23 at 15:59
  • 1
    Pacific/Pago_Pago is an alias for Pacific/Samoa, or at least it is also 26 July there. – Mark Rotteveel Jul 26 '23 at 16:03