IST在Java中进行时间转换 [英] IST to EST Time Conversion In Java

查看:237
本文介绍了IST在Java中进行时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IST时间里有一个Java日期字段。我想转换为EST时间,输出应该仅作为日期类型。我可以使用以下代码完成相同的操作: -

  SimpleDateFormat dateTimeFormat = new SimpleDateFormat(MM / dd / yyyy HH:mm:ss); 
dateTimeFormat.setTimeZone(TimeZone.getTimeZone(Asia / Calcutta));
Date date = new Date();
DateFormat timeFormat = new SimpleDateFormat(MM / dd / yyyy HH:mm:ss);
timeFormat.setTimeZone(TimeZone.getTimeZone(America / New_York));
String estTime = timeFormat.format(date);
date = new SimpleDateFormat(MM / dd / yyyy HH:mm:ss,Locale.ENGLISH).parse(estTime);

上述代码的问题是,虽然日期在EST时间转换,但时间区域仍然显示为IST而不是EST。其余的日期转换完好。有没有办法在日期字段中明确设置时区到EST。对此的任何帮助将非常感谢。



-Subhadeep

解决方案

日期类是时区不可知的。基本上,它总是基于GMT,但是当它打印时,它使用当前的系统时区进行调整。



但是,日历是具体的时区。请参阅日历。 setTimeZone()



考虑:

 日历cal = new GregorianCalendar(); 
cal.setTimeZone(TimeZone.getTimeZone(America / New_York));
cal.setTime(new Date());


I have a Date field in Java in IST Time. I want to convert the same to EST Time and the output should be as a Date Type only. I am able to accomplish the same using the below piece of code:-

SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
Date date = new Date();
DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String estTime = timeFormat.format(date);
date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(estTime);

The problem with the above piece of code is that though the date is converted in EST Time, the Time Zone is still showing as IST and not EST. The rest of the date is converted perfectly fine. Is there any way to explicitly set the time Zone To EST in the Date Field.Any help regarding this will be highly appreciated.

-Subhadeep

解决方案

The Date class is time-zone agnostic. Basically, it is always based on GMT although when it is printed it uses the current system time zone to adjust it.

However, Calendar is time-zone specific. See Calendar.setTimeZone().

Consider:

   Calendar cal = new GregorianCalendar();
   cal.setTimeZone(TimeZone.getTimeZone("America/New_York"));
   cal.setTime(new Date());

这篇关于IST在Java中进行时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆