{"id":2728,"date":"2011-02-09T23:50:30","date_gmt":"2011-02-09T18:20:30","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2728"},"modified":"2015-05-19T17:46:17","modified_gmt":"2015-05-19T12:16:17","slug":"converting-date-from-one-timezone-to-another-in-groovy","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/converting-date-from-one-timezone-to-another-in-groovy\/","title":{"rendered":"Converting date from one timezone to another in groovy"},"content":{"rendered":"<p>Hi,<br \/>\nIn my recent grails project, i came across the situation where i needed to convert the date in given timezone to the date in another timezone. I searched a lot about it and got many solutions for this problem and then i came out with a simple way to do so.<\/p>\n<p>Lets i have a date in TimeZone say oldTimeZone and i want to convert it to another timeZone say newTimeZone, so to convert it to another timezone, i wrote the method given below.<\/p>\n<p>&nbsp;<\/p>\n<p>[java]<br \/>\npublic Date convertToNewTimeZone(Date date, TimeZone oldTimeZone, TimeZone newTimeZone){<\/p>\n<p>      long oldDateinMilliSeconds=date.time &#8211; oldtimeZone.rawOffset<br \/>\n      \/\/ oldtimeZone.rawOffset returns the difference(in milliSeconds) of time in that timezone with the time in GMT<br \/>\n      \/\/ date.time returns the milliseconds of the date<\/p>\n<p>      Date dateInGMT=new Date(oldDateinMilliSeconds)<\/p>\n<p>      long convertedDateInMilliSeconds = dateInGMT.time + newTimeZone.rawOffset<br \/>\n      Date convertedDate = new Date(convertedDateInMilliSeconds)<\/p>\n<p>    return convertedDate<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>This Works for me.<br \/>\nHope it helps.<\/p>\n<p>Cheers..!!!<br \/>\nVishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In my recent grails project, i came across the situation where i needed to convert the date in given timezone to the date in another timezone. I searched a lot about it and got many solutions for this problem and then i came out with a simple way to do so. Lets i have [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":42},"categories":[7],"tags":[718,713,714,715,717,716],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2728"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2728"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2728\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}