Roy Tang
Roy Tang

Reputation: 5771

How to convert an offset from GMT to the local timezone?

I have two inputs:

Using Java, how can I convert the given date value into the corresponding date/time in the local timezone? There doesn't seem to be any timezone offset function in the Date class.

Thanks!

Upvotes: 1

Views: 297

Answers (2)

Abs
Abs

Reputation: 3962

I think you need to use the TimeZone.getAvailableIDs(rawOffsetinMiliSeconds) to get a timezone value. Working Example:

Date now = new Date();  
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
sdf1.setTimeZone(TimeZone.getTimeZone("UTC"));  
System.out.println(sdf1.format(now));  
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
sdf2.setTimeZone(TimeZone.getTimeZone((TimeZone.getAvailableIDs(5*1000*3600))[0]));  
System.out.println(sdf2.format(now));  

Upvotes: 0

weston
weston

Reputation: 54801

(If you don't want to use JodaTime) Use TimeZone with setRawOffset with code from this answer: https://stackoverflow.com/a/19378721/360211

Upvotes: 1

Related Questions