Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java set timezone at runtime

I am working on a desktop application. It would get input as a text file from user having contents like this :

..................................   
..................................

Mon Jul  9 14:41:07 MDT 2012
..................................
..................................
..................................

I am using this information and creating a timeseries chart using jfreechart library. Timezone could be anything available in the world. But when I use this file its default timezone is sytem's timezone(IST) so doesn't show MDT time. When I tried to capture timezone from date and then used

TimeZone.setDefault(TimeZone.getTimeZone("MDT"));

It didn't work. How can I change the default timezone in Java when I am having abbreviation for timezone like MDT, CDT etc?

like image 545
dirtyhandsphp Avatar asked Apr 15 '26 18:04

dirtyhandsphp


2 Answers

MDT is not the timezone key, it is the short display name of the timezone, so TimeZone.getTimeZone("MDT") would return default time zone which is GMT. The keys for Mountain Time are MST,MST7MDT etc. So, you need to identify the key of the timezone. Please note there are many different keys for the same short display name e.g. for MDT shortName there are keys with US/Mountain, US/Arizona, SystemV/MST7MDT, Navajo, Mexico/BajaSur, MST7MDT and MST.

like image 52
vikas Avatar answered Apr 17 '26 07:04

vikas


Theres no timezone called MDT, it is MST7MDT. Use:

TimeZone.setDefault(TimeZone.getTimeZone("MST7MDT"));

Also see Java's java.util.TimeZone

like image 26
Reimeus Avatar answered Apr 17 '26 07:04

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!