IST在Java中进行时间转换 [英] IST to EST Time Conversion In Java
问题描述
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屋!