2015-03-19 65 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); 
} 

} 

} 
相關問題