Reputation: 1671
I have the following Java code:
Date convertedDate = Date.from(zonedDateTime.toInstant());
The Issue I am having is that convertedDate
is not correct in comparison to the zonedDateTime object.
For Example when I have a zonedDateTime object of:
2021-09-16T12:00
with zone:
Africa/Abidjan
The code above converts this to:
Thu Sep 16 13:00:00 BST 2021
What I would be expecting here is
Thu Sep 16 10:00:00 BST 2021
As the Africa/Abidjan
timezone is 2 hours ahead of BST.
How can I solve this?
Upvotes: 6
Views: 2575
Reputation: 338566
Answer by Avinash is correct. Here are a few more thoughts.
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.
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.
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
Upvotes: 4
Reputation: 79075
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time
, the modern Date-Time API:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
// The given ZonedDateTime
ZonedDateTime zdtAbidjan = ZonedDateTime.of(
LocalDateTime.of(LocalDate.of(2021, 9, 16),
LocalTime.of(12, 0)),
ZoneId.of("Africa/Abidjan")
);
System.out.println(zdtAbidjan);
ZonedDateTime zdtLondon = zdtAbidjan.withZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println(zdtLondon);
}
}
Output:
2021-09-16T12:00Z[Africa/Abidjan]
2021-09-16T13:00+01:00[Europe/London]
The Z
in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC
timezone (which has the timezone offset of +00:00
hours).
From the output, it is clear that 2021-09-16T12:00Z[Africa/Abidjan] is equal to 2021-09-16T13:00+01:00[Europe/London].
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 5