2011-05-26 96 views
0

在我的應用程序中,我使用GeoNamesAPI在任何位置獲取當前時間。 我已經註冊使用。如何獲取當前GMT值?

我的代碼如下所示:

Timezone currentTimeZone; 
org.geonames.WebService.setUserName("mathew"); 
currentTimeZone = GeoNamesAPI.fetchTimeZone(latitude, longitude); 

問題是,當我在任何海洋檢查時間,這currentTimeZone返回null。 所以在這種情況下,我顯示了GMT值。

String time = null; 
Integer timeZone = (int) (((longitude/7.5) + 1)/2); 

if (timeZone >= 0) { 
    time = "GMT+" + timeZone; 
} else { 
    time = "GMT" + timeZone; 
} 

因此time值將是那種GMT + somevalue。我想爲這種情況找到另一種解決方案。在這種情況下,我也想顯示時間值。我怎樣才能做到這一點?有沒有辦法獲得GMT值?注意:我不想顯示日期,只需要時間。

在此先感謝。

回答

0

我得到了它的工作。代碼如下:

Integer timeZone = (int) (((longitude/7.5) + 1)/2); 
DateFormat dateformat = DateFormat.getTimeInstance(DateFormat.SHORT); 
dateformat.setTimeZone(TimeZone.getTimeZone("gmt")); 
Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.HOUR, timeZone); //Adding/Subtracting hour to current date time 
String newdate = dateformat.format(cal.getTime());