我想要一个能够以下面方式工作的代码。
显示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