2015-03-19 64 views
1

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

} 
+1

不要使用'Calendar'或'Date'。使用java 8时间API或jodatime。 – 2015-03-19 06:22:16

+0

我工作的服务器上没有Java 8,我们目前只在Java 6上,所以请提出相应建议 – user3058875 2015-03-19 07:33:15

+1

Jodatime是。 – 2015-03-19 07:49:30

回答

0
import java.time.LocalTime; 
import java.time.ZoneId; 

public class Main { 
    public static void main(String... args) { 
     ZoneId zone2 = ZoneId.of("America/Santiago"); 
    LocalTime now2 = LocalTime.now(zone2); 
    System.out.println(now2); 
    } 
} 

Use the below link to know the timezones 

http://joda-time.sourceforge.net/timezones.html

+0

将其转换为不同的时区。但上述代码在Java8中有效吗?有谁知道java6的解决方案 – user3058875 2015-03-20 04:21:23

0
package com.java.test; 

import java.util.Calendar; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.TimeZone; 

import javax.swing.text.ZoneView; 

public class TestJavaCode { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
// TODO Auto-generated method stub 
try 
{ 
Calendar cal = GregorianCalendar.getInstance(); 
TimeZone tzone = cal.getTimeZone(); 
tzone.useDaylightTime(); 
System.out.println("Timezone:"+tzone.getDisplayName()); 


TimeZone tzone1 = TimeZone.getTimeZone("Canada/Central"); 
TimeZoneIDProvide.getTimeZoneID(TimeZoneID.US_EASTERN).getTimeZone(); 

Calendar cal1 = new GregorianCalendar(tzone1); 
cal1.setTimeInMillis(cal.getTimeInMillis()); 

System.out.println("Time in US Hour: "+cal1.get(Calendar.HOUR)); 
System.out.println("Time in US Min: "+cal1.get(Calendar.MINUTE)); 
System.out.println("Time in US Sec: "+cal1.get(Calendar.SECOND)); 



} 
catch(Exception e) 
{ 
System.out.println("Exception caught:"+e); 
} 

} 

} 
相关问题