我写这段代码将当前系统日期和时间转换为其他时区。我没有收到任何错误,但我没有按预期得到我的输出。就像如果我在一个特定的时间执行我的计划。我的输出为::日期和时间转换为其他一些时区在java
在印度现在时间是:: Fri Feb 24 16:09:23 IST 2012
在:: 中部标准时间的日期和时间: :根据CST时区Sat Feb 25 03:39:23 IST 2012
与实际时间是::
Friday, 24 February 4:39:16 a.m(GMT - 6:00)
所以有一些时间差距。我不知道为什么会发生这种情况。任何帮助将不胜感激。该代码::
package MyPackage;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Temp2 {
public static void main(String[] args) {
try {
Calendar currentdate = Calendar.getInstance();
String strdate = null;
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
strdate = formatter.format(currentdate.getTime());
TimeZone obj = TimeZone.getTimeZone("CST");
formatter.setTimeZone(obj);
//System.out.println(strdate);
//System.out.println(formatter.parse(strdate));
Date theResult = formatter.parse(strdate);
System.out.println("The current time in India is :: " +currentdate.getTime());
System.out.println("The date and time in :: "+ obj.getDisplayName() + "is ::" + theResult);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
夏令效果? – Nishant 2012-02-24 10:44:59
使用java.util处理时间很快就会变得很麻烦,如果您有一些时间,请查看joda-time API – angryInsomniac 2012-02-24 10:50:34
看看这个http://stackoverflow.com/questions/2356672/date-parsing-formating-with -timezone-and-simpledateformat-problem-around-dst-swi – Pavan 2012-02-24 10:50:36