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?
Advertisement
Answer
java.time
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 theEtc/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.