我們有一個web服務應用程序,總是需要時間的投入,它的UTC格式帶時區的Java日期祕密更改?
2012-12-06T05:00:00.000Z
,這裏是用來解析日期到Java util的代碼Date對象
private static final Pattern PATTERN = Pattern.compile(
"(\\d{4})(?:-(\\d{2}))?(?:-(\\d{2}))?(?:[Tt](?:(\\d{2}))?(?::(\\d{2}))?(?::(\\d{2}))?(?:\\.(\\d{3}))?)?([Zz])?(?:([+-])(\\d{2}):(\\d{2}))?");
Matcher m = PATTERN.matcher(dateString);
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
int hoff = 0, moff = 0, doff = -1;
if (m.group(9) != null) {
doff = m.group(9).equals("-") ? 1 : -1;
hoff = doff * (m.group(10) != null ? Integer.parseInt(m.group(10)) : 0);
moff = doff * (m.group(11) != null ? Integer.parseInt(m.group(11)) : 0);
}
c.set(Calendar.YEAR, Integer.parseInt(m.group(1)));
c.set(Calendar.MONTH, m.group(2) != null ? Integer.parseInt(m.group(2))-1 : 0);
c.set(Calendar.DATE, m.group(3) != null ? Integer.parseInt(m.group(3)) : 1);
c.set(Calendar.HOUR_OF_DAY, m.group(4) != null ? Integer.parseInt(m.group(4)) + hoff: 0);
c.set(Calendar.MINUTE, m.group(5) != null ? Integer.parseInt(m.group(5)) + moff: 0);
c.set(Calendar.SECOND, m.group(6) != null ? Integer.parseInt(m.group(6)) : 0);
c.set(Calendar.MILLISECOND, m.group(7) != null ? Integer.parseInt(m.group(7)) : 0);
return c.getTime();
最近觀察到一個奇怪的事情,作爲應用程序第一次啓動時,返回的日期將只打印正確的 週四12月6日00:00:00 EST 2012
因爲我們在EST時區。然後一段時間後,執行一段時間後,即使沒有重新啓動應用程序,同一日期將被打印爲 Thur Dec 06 05:00:00 UTC 2012
我一直在挖掘應用程序,我不查看會重置我們應用程序的默認時區的任何更改。這怎麼會發生?這是一個星期以來,我們開始做這項工作,我們仍然一無所知:-(
而且,反正是有以確保應用程序保持使用該系統時區爲不會改變?
感謝很多關於任何幫助/提示
你爲什麼不使用javax.text.SimpleDateFormat.parse(String)??? –
也發佈了https://forums.oracle.com/forums/thread.jspa?threadID=2452390&tstart=0 – jtahlborn
這是自第一天以來我們一直在使用的一些第三方代碼。 – gigi2