Posts

Showing posts from April, 2015

java convert date from one timezone to another

Use Case: Suppose you receive time string from one machine in  PST zone as "04-30-2015 07:00:00" , the machine on which you receive this string data is in CST and you want to convert the time in EST . If we use java.util.Date it will change the string into Date with reference to local machine timezone which is CST. i.e. it will look something like " 04-30-2015 07:00:00 CST ", so the conversion will something like below. What will Happen Time is EST  will be calculated as " 04-30-2015 09:00:00 EST " What is the perceived output Time is EST  should be  " 04-30-2015 10:00:00 EST " Why It happens java.util.Date does not take in reference of the timezone, for it time is absolute and Timezone is only interpreted as display property, and offset is applied on absolute time to print the timezone accordingly. Solution Use Joda time. If you are using maven include the following dependency.  <dependency>             <groupId>