我寫這段代碼將當前系統日期和時間轉換爲其他時區。我沒有收到任何錯誤,但我沒有按預期得到我的輸出。就像如果我在一個特定的時間執行我的計劃。我的輸出爲::日期和時間轉換爲其他一些時區在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