java设置时区,Java在运行时设置时区

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?

解决方案

Thanks Guys. Thanks for quick responses. @vikas your response proves to be more useful. I am using following code and it worked well.

String timezoneLongName = "";

String fileTimeZone = "MDT"; //timezone could be anything, getting from file.

Date date = new Date();

String TimeZoneIds[] = TimeZone.getAvailableIDs();

for (int i = 0; i < TimeZoneIds.length; i++) {

TimeZone tz = TimeZone.getTimeZone(TimeZoneIds[i]);

String tzName = tz.getDisplayName(tz.inDaylightTime(date),TimeZone.SHORT);

if(fileTimeZone.equals(tzName)){

timezoneLongName = TimeZoneIds[i];

break;

}

}

if(timezoneLongName != null && !timezoneLongName.isEmpty() && !timezoneLongName.trim().isEmpty() && timezoneLongName.length() != 0){

TimeZone.setDefault(TimeZone.getTimeZone(timezoneLongName));

}

Although there are more than one entries for "MDT" timezone but it resolves my problem without any problem at first match itself. I have tested code on CDT, MDT and CDT timezones and it worked really well.Thanks Guys!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值