2012-02-24 35 views
6

我寫這段代碼將當前系統日期和時間轉換爲其他時區。我沒有收到任何錯誤,但我沒有按預期得到我的輸出。就像如果我在一個特定的時間執行我的計劃。我的輸出爲::日期和時間轉換爲其他一些時區在java

印度現在時間是:: Fri Feb 24 16:09:23 IST 2012

在:: 中部標準時間的日期和時間: :根據CST時區Sat Feb 25 03:39:23 IST 2012

與實際時間是::

Friday, 24 February 4:39:16 a.m(GMT - 6:00) 

所以有一些時間差距。我不知道爲什麼會發生這種情況。任何幫助將不勝感激。該代碼::

package MyPackage; 

import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 

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

public class Temp2 { 


    public static void main(String[] args) { 

     try { 
      Calendar currentdate = Calendar.getInstance(); 
      String strdate = null; 
      DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
      strdate = formatter.format(currentdate.getTime()); 
      TimeZone obj = TimeZone.getTimeZone("CST"); 

      formatter.setTimeZone(obj); 
      //System.out.println(strdate); 
      //System.out.println(formatter.parse(strdate)); 
      Date theResult = formatter.parse(strdate); 

      System.out.println("The current time in India is :: " +currentdate.getTime()); 

      System.out.println("The date and time in :: "+ obj.getDisplayName() + "is ::" + theResult); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+2

夏令效果? – Nishant 2012-02-24 10:44:59

+1

使用java.util處理時間很快就會變得很麻煩,如果您有一些時間,請查看joda-time API – angryInsomniac 2012-02-24 10:50:34

+0

看看這個http://stackoverflow.com/questions/2356672/date-parsing-formating-with -timezone-and-simpledateformat-problem-around-dst-swi – Pavan 2012-02-24 10:50:36

回答

15

它通過網絡。可能已經google了。不管怎麼說,這裏是你的一個版本(無恥地挑選和here修改):

Calendar calendar = Calendar.getInstance(); 
TimeZone fromTimeZone = calendar.getTimeZone(); 
TimeZone toTimeZone = TimeZone.getTimeZone("CST"); 

calendar.setTimeZone(fromTimeZone); 
calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1); 
if (fromTimeZone.inDaylightTime(calendar.getTime())) { 
    calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1); 
} 

calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset()); 
if (toTimeZone.inDaylightTime(calendar.getTime())) { 
    calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings()); 
} 

System.out.println(calendar.getTime()); 
+0

Thanx nishant ..的答案..我得到的時間ryt ..但日期仍然不正確..它給我..25日而不是24日...請如果你能得到那個ryt .. ?? thanx – 2012-02-24 11:16:02

+1

@ user1230183更新了答案。 – Nishant 2012-02-24 11:31:20

+0

Thanx nishant ..問題解決.. :) .. thanx很多.. :) – 2012-02-24 11:38:23

7

你的錯誤是調用parse而不是format

您可以致電parse從字符串中解析日期,但在您的情況下,您已經有日期並需要使用正確的時區格式化日期。

Calendar currentdate = Calendar.getInstance(); 
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
TimeZone obj = TimeZone.getTimeZone("CST"); 
formatter.setTimeZone(obj); 
System.out.println("Local:: " +currentdate.getTime()); 
System.out.println("CST:: "+ formatter.format(currentdate.getTime()) 

替換你的代碼,我希望你會得到你期望的輸出。

+0

我試過了..但它給了我一個異常java.lang.IllegalArgumentException:無法格式化給定的對象作爲日期 – 2012-02-24 11:17:08

+0

對不起,我需要提供的不止一個行 - 答案更新 – 2012-02-24 11:18:25

3

處理Java中的日期在我的日常工作是一個不平凡的任務。我建議你使用Joda-Time來簡化我們的編碼時間,你不必「重新發明輪子」。

+0

thanx達里奧..對於這個建議..肯定會把我的代碼這樣..看看是否有用... – 2012-02-24 11:20:12

+0

Joda-時間主頁已經移動。所以我更新了鏈接。 – naXa 2017-12-15 09:40:28

0

問題是當您打印日期obj時,它會調用toString方法,並且它會在您的機器默認時區中打印。試試這段代碼,看看不同之處。

Calendar currentdate = Calendar.getInstance(); 
String strdate = null; 
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ssz"); 
strdate = formatter.format(currentdate.getTime()); 
System.out.println("strdate=>" + strdate); 
TimeZone obj = TimeZone.getTimeZone("CST"); 

formatter.setTimeZone(obj); 
strdate = formatter.format(currentdate.getTime()); 
Date theResult = formatter.parse(strdate); 

System.out.println("The current time in India is :: " +currentdate.getTime()); 

System.out.println("The date and time in :: " + obj.getDisplayName() + "is ::" + theResult); 
System.out.println("The date and time in :: " + obj.getDisplayName() + "is ::" + strdate); 
+0

Thanx abhutra ..我gt從你的代碼ryt ... :) – 2012-02-24 11:41:45

+0

你是否得到了你正在做的錯誤..? – 2012-02-24 11:54:30

+0

嗯,不完全...但是你說什麼,似乎我打印的日期沒有轉換到CST時區..它將不勝感激,如果你會解釋我哪裏出錯了。?? – 2012-02-24 12:03:37

0

嘗試是這樣的

Date date = new Date(); 
String formatPattern = ....; 
SimpleDateFormat sdf = new SimpleDateFormat(formatPattern); 

TimeZone T1; 
TimeZone T2; 

// set the Calendar of sdf to timezone T1 
sdf.setTimeZone(T1); 
System.out.println(sdf.format(date)); 

// set the Calendar of sdf to timezone T2 
sdf.setTimeZone(T2); 
System.out.println(sdf.format(date)); 

// Use the 'calOfT2' instance-methods to get specific info 
// about the time-of-day for date 'date' in timezone T2. 
Calendar calOfT2 = sdf.getCalendar(); 
+1

這很好解釋你的答案。 – 2014-10-14 12:00:52

0

你可以用「CST6CDT」 因爲在一些國家,它們遵循CDT在夏季和CST冬季

public static String getDateInCST() { 
      Calendar calendar = Calendar.getInstance(); 
      DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
      formatter.setTimeZone(TimeZone.getTimeZone("CST6CDT")); 
      String strdate = formatter.format(calendar.getTime()); 
      TimeZone.getAvailableIDs(); 
      return strdate; 
    } 
相關問題