Answer by Avinash is correct. Here are a few more thoughts.
Time zone names
BST
is not a real time zone name. Perhaps you meant Europe/London
. And that is not GMT/UTC. The London time zone can vary in its offset, because of Daylight Saving Time (DST) and perhaps other reasons.
UTC
Let's look at your moment in each of the three different time zones.
First we parse your input as a LocalDateTime
, lacking the context of a time zone or offset-from-UTC. Then we assign a time zone for Abidjan as the context to produce a ZonedDateTime
object. We adjust to another time zone, resulting in a second ZonedDateTime
that represents the same moment, the same point on the timeline, but a different wall-clock time. Lastly, we extract a Instant
to effectively adjust into UTC. A Instant
represents a moment in UTC, always in UTC.
LocalDateTime ldt = LocalDateTime.parse( "2021-09-16T12:00" ) ;
ZonedDateTime zdtAbidjan = ldt.atZone( ZoneId.of( "Africa/Abidjan" ) ) ;
ZonedDateTime zdtLondon = zdtAbidjan.withZoneSameInstant( ZoneId.of( "Europe/London" ) ) ;
Instant instant = zdtAbidjan.toInstant() ; // Adjust to UTC by extracting an `Instant` object.
See this code run live at IdeOne.com.
ldt: 2021-09-16T12:00
zdtAbidjan: 2021-09-16T12:00Z[Africa/Abidjan]
zdtLondon: 2021-09-16T13:00+01:00[Europe/London]
instant: 2021-09-16T12:00:00Z
The Z
at the end means an offset of zero hours-minutes-seconds, pronounced “Zulu”. So we can zee that noon on that September day in Côte d'Ivoire is the same as in UTC, having an offset of zero. In contrast, the +01:00
tells us that London time is an hour ahead. So the clock reads 1 PM (13:00
) rather than noon.
Fetch the offset
You can determine the offset in effect at a particular moment via the ZoneRules
class. The offset info is represented by the ZoneOffset
class.
ZoneId z = ZoneId.of( "Africa/Abidjan" ) ;
ZoneRules rules = z.getRules() ;
ZoneOffset offset = rules.getOffset( LocalDateTime.parse( "2021-09-16T12:00" ) ) ;
int offsetInSeconds = offset.getTotalSeconds() ;
Or condense that to:
ZoneId
.of( "Africa/Abidjan" )
.getRules()
.getOffset( LocalDateTime.parse( "2021-09-16T12:00" ) )
.getTotalSeconds()
When run we see again that Côte d'Ivoire is using an offset of zero at that date-time.
rules: ZoneRules[currentStandardOffset=Z]
offset: Z
offsetInSeconds: 0