我想要一個能夠以下面方式工作的代碼。
顯示8-10個城市的DST應自動處理的時間。在JS/Java中的任何城市都有DST的時間
For example : Melbourne observes DST, a API using which I can fetch the current time based on location without any hardcode done for DST calculation.
目前它是基於硬編碼values.It使用的代碼採系統時間轉換爲GMT,然後添加/基於位置偏移減去。
然後根據日期計算DST並適當添加。
但我想用一些API來刪除硬編碼的值,我嘗試使用下面的代碼。
The below code giving time for IST/GMT, but when I put AEST/AEDT it doesnt give the correct time.
public class DateFormatter {
public static String formatDateToString(Date date, String format,
String timeZone) {
// null check
if (date == null) return null;
// create SimpleDateFormat object with input format
SimpleDateFormat sdf = new SimpleDateFormat(format);
// default system timezone if passed null or empty
if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
timeZone = Calendar.getInstance().getTimeZone().getID();
}
// set timezone to SimpleDateFormat
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
// return Date in required format with timezone as String
return sdf.format(date);
}
public static void main(String[] args) {
//Test formatDateToString method
Date date = new Date();
System.out.println("Default Date:"+date.toString());
System.out.println("System Date: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", null));
System.out.println("System Date in PST: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "PST"));
System.out.println("System Date in IST: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "IST"));
System.out.println("System Date in GMT: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "GMT"));
System.out.println("System Date in CT: "+formatDateToString(date, "dd MMM yyyy hh:mm:ss a", "BDT"));
}
}
不要使用'Calendar'或'Date'。使用java 8時間API或jodatime。 – 2015-03-19 06:22:16
我工作的服務器上沒有Java 8,我們目前只在Java 6上,所以請提出相應建議 – user3058875 2015-03-19 07:33:15
Jodatime是。 – 2015-03-19 07:49:30