不知何故我報告了一個問題,其中以下代碼將來會提供日期。java日曆/格式化程序提供了錯誤的日期
所用的時區爲格林尼治標準時間+01:00。 numberOfDays是非負整數。
此代碼的意圖是減少當前日期的天數。
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yy",Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -numberOfDays);
Date date = calendar.getTime();
String dateStr= formatter.format(date);
System.out.println("Date : "+dateStr);
我無法在我的機器上重現此操作。
區域設置是否影響時區?
我試圖關聯到Why does a new SimpleDateFormat object contain calendar with the wrong year?和Strange problem with timezone, calendar and SimpleDateFormat,但徒勞無功。
請幫我理解並糾正這個問題。
適合我的作品(打印日期:2月25日11日爲我;我在格林威治標準時間)。你的系統在哪個時區? – adarshr 2011-03-07 11:40:20
只是爲了澄清:在哪裏能夠在本地重現問題?或者你剛收到未經驗證的報告? – 2011-03-07 11:42:16
也許應該是DAY_OF_YEAR而不是DAY_OF_MONTH? – 2011-03-07 11:44:28