Reputation: 3748
I have a GMT timestamp coming from server as below:
2021-05-27T07:46:31+00:00
I need to convert this to CET / Belgium time stamp. CET will be 2 hours ahead of GMT. How to convert this in Android?
Upvotes: 0
Views: 619
Reputation: 338835
Your input carries an offset-from-UTC but not a time zone. A time zone is a history of the past, present, and future changes to the offset used by the people of a particular region.
A real time zone has a name in the format of Continent/Region
. Never use the 2-4 character pseudo zones such as CET
, CST
, and IST
, as these are not real time zones, are not standardized, and are not even unique.
OffsetDateTime odt = OffsetDateTime.parse( "2021-05-27T07:46:31+00:00" ) ;
ZoneId z = ZoneId.of( "Europe/Brussels" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
Upvotes: 1