2014-04-02 112 views
1

兩種不同的結果我收到的02.04.201406日期字符串:26:06我轉換成另一個日期SimpleDateFormat的返回了相同的輸入

public static final SimpleDateFormat SDF_DATE_TIME_RECEIVED = new SimpleDateFormat("dd-MM-yy HH:mm:ss"); 
static{ 
    SDF_DATE_TIME_RECEIVED.setTimeZone(TimeZone.getTimeZone("IST")); 
} 
SimpleDateFormat originalDateFormat = new SimpleDateFormat("dd.MM.yyyyHH:mm:ss"); 
originalDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 
Date date = originalDateFormat.parse(dateString); 
String newDateString = SDF_DATE_TIME_RECEIVED.format(date)); 

這正確執行,並給我的日期02-04- 14 11點56分06秒

現在我使用此newDateString再次產生兩個不同的格式如下圖所示:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy"); 
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); 
Date date = SDF_DATE_TIME_RECEIVED.parse(newDateString); 
String datePart = dateFormat.format(date); 
String timePart = timeFormat.format(date); 

現在的問題是生產服務器上dateString上述我得到的輸出:

newDateString = "04/02/2014 11:56:06" 
datePart = "04/02/70" 
timePart = "00:56:06" 

我試圖重新對開發環境的場景,但我得到了正確的結果:即使在通過重新發送請求做生產環境相同的過程

newDateString = "04/02/2014 11:56:06" 
datePart = "04/02/14" 
timePart = "11:56:06" 

出現正確的輸出。

這裏可能會出現什麼問題?這不是一個孤立的問題。這種情況發生在很多請求上,而且日期不正確的情況幾乎是隨機的

+2

什麼是開發和生產的默認時區?此外,SimpleDateFormat不是線程安全的,因此如果可以在多線程環境中使用它們,則不應創建靜態實例。 – MrPotes

+1

嘗試隔離它是解析還是格式化。您正在執行兩個解析操作和三個格式操作 - 如果您可以將其修復爲'new Date(SOME_CONSTANT_LONG_VALUE);字符串文本= format.format(日期);'仍然顯示差異,這將使它更容易幫助你。另外請注意,您並未在'dateFormat'和'timeFormat'中設置時區,因此您將獲得系統默認時區。 (你甚至可能會得到系統默認的日曆 - 我不確定。)最後,'SimpleFormat'不是線程安全的...... –

+0

第一個結果'04 -02-14 11:56:06'是錯的,它應該是'02-04-14 11:56:06'(兩種格式都有日年月訂單) –

回答

1

內部SimpleDateFormat是有狀態的,因此使它成爲靜態最終並不能解決多線程問題。如果您的代碼被多個請求調用,則SDF_DATE_TIME_RECEIVED會給出損壞的結果。這是這個問題的第一個嫌疑犯(特別是因爲錯誤似乎是隨機的),請嘗試將其更改爲局部變量。

另一個想法:你沒有在dateFormat和timeFormat上設置任何時區。它看起來像你期望是印度標準時間(IST),所以你應該這樣做:

public static void main(String[] args) throws Exception { 

    SimpleDateFormat SDF_DATE_TIME_RECEIVED = new SimpleDateFormat("dd-MM-yy HH:mm:ss"); 
    SDF_DATE_TIME_RECEIVED.setTimeZone(TimeZone.getTimeZone("IST")); 

    SimpleDateFormat originalDateFormat = new SimpleDateFormat("dd.MM.yyyyHH:mm:ss"); 
    originalDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 

    String dateString = "02.04.201406:26:06"; 
    Date date1 = originalDateFormat.parse(dateString); 
    String newDateString = SDF_DATE_TIME_RECEIVED.format(date1); 

    System.out.println(newDateString); 

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy"); 
    dateFormat.setTimeZone(TimeZone.getTimeZone("IST")); 
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); 
    timeFormat.setTimeZone(TimeZone.getTimeZone("IST")); 

    Date date2 = SDF_DATE_TIME_RECEIVED.parse(newDateString); 
    String datePart = dateFormat.format(date2); 
    String timePart = timeFormat.format(date2); 

    System.out.println("datePart=" + datePart); 
    System.out.println("timePart=" + timePart); 
} 
+0

我會接受這個答案,如果它的工作......做測試.... – gladiator

+2

BTW IST意味着印度標準時間:) – Skabdus

+0

糟糕,我的壞。固定。 –

相關問題