Thursday, August 1, 2019

date - How to convert ISO 8601 formatted DateTime to another time zone in Java?

My current date:
Date utc: 2018-06-06T16:30:00Z (ISO 8601 in UTC)
OR
Date iso: 2018-06-06T11:30:00-05:00 (ISO 8601)
OR
Date epoch: 1528302600000 (Epoch/Unix Timestamp)




I wish to convert the above DateTime to some another time zone areas (like GMT+5:30). And I'm not sure which time format I'll receive from above three. So can I've a generic method which can convert above to some another time zone returning java.util.Date in Java 8?



I did Something like this, But it didn't worked out



public Date convertDateToLocalTZ(Date iso8601, ZoneId toZoneId) {
Date dateTime = null;
if (iso8601 != null && toZoneId != null) {
Instant instant = iso8601.toInstant();
LocalDateTime localDateTime = instant.atZone(toZoneId).toLocalDateTime();
dateTime = Date.from(localDateTime.atZone(toZoneId).toInstant());

return dateTime;
}
return dateTime;
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...