我正在開發一個函數來比較一些sqlite存儲日期的星期編號與當前日期的星期編號。Android日曆返回錯誤的月份編號
但是Calendar類的set(y,m,d)方法總是返回一個錯誤的月份數的日期,在我的情況下它返回的月份設置爲7月而不是6月。
這裏是我的代碼片段:
//Date depuis SQLite/Grab date from SQLite
String[] tokens = extraireTokensDate(s.getDateHeure());
//Comparaison semaine/Week compare
Calendar cal1 = Calendar.getInstance();
Date date = new Date();
cal1.setTime(date);
Log.d("DEBUG", "DATE SQL : "+Integer.parseInt(tokens[0])+" "+Integer.parseInt(tokens[1])+" "+Integer.parseInt(tokens[2]));
Calendar cal2 = Calendar.getInstance();
cal2.set(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]));
SimpleDateFormat sdf = new SimpleDateFormat("W) dd/MM/yyyy");
Log.d("DEBUG", "(Actuel/Now) DATE : "+sdf.format(cal1.getTime()));
Log.d("DEBUG", "(Actuel/Now) Numero de semaine (annee/year) : "+cal1.get(Calendar.WEEK_OF_YEAR));
Log.d("DEBUG", "(Passe/Past) DATE : "+sdf.format(cal2.getTime()));
Log.d("DEBUG", "(Passe/Past) Numero de semaine (annee/year) : "+cal2.get(Calendar.WEEK_OF_YEAR));
調試器的輸出(日期是法國格式化)
06-12 19:32:37.035: DEBUG/DEBUG(5777): DATE SQL : 2011 6 12
06-12 19:32:37.045: DEBUG/DEBUG(5777): (Actuel/Now) DATE : 3) 12/06/2011
06-12 19:32:37.045: DEBUG/DEBUG(5777): (Actuel/Now) Numero de semaine/Week number (annee/year) : 24
06-12 19:32:37.055: DEBUG/DEBUG(5777): (Passe/Past) DATE : 3) 12/07/2011
06-12 19:32:37.055: DEBUG/DEBUG(5777): (Passe/Past) Numero de semaine/Week number (annee/year) : 28
06-12 19:32:37.065: DEBUG/DEBUG(5777): DATE SQL : 2011 6 11
06-12 19:32:37.075: DEBUG/DEBUG(5777): (Actuel/Now) DATE : 3) 12/06/2011
06-12 19:32:37.075: DEBUG/DEBUG(5777): (Actuel/Now) Numero de semaine/Week number (annee/year) : 24
06-12 19:32:37.075: DEBUG/DEBUG(5777): (Passe/Past) DATE : 3) 11/07/2011
06-12 19:32:37.075: DEBUG/DEBUG(5777): (Passe/Past) Numero de semaine/Week number (annee/year) : 28
06-12 19:32:37.095: DEBUG/DEBUG(5777): DATE SQL : 2011 6 7
06-12 19:32:37.105: DEBUG/DEBUG(5777): (Actuel/Now) DATE : 3) 12/06/2011
06-12 19:32:37.105: DEBUG/DEBUG(5777): (Actuel/Now) Numero de semaine/Week number (annee/year) : 24
06-12 19:32:37.105: DEBUG/DEBUG(5777): (Passe/Past) DATE : 2) 07/07/2011
06-12 19:32:37.105: DEBUG/DEBUG(5777): (Passe/Past) Numero de semaine/Week number (annee/year) : 27
06-12 19:32:37.125: DEBUG/DEBUG(5777): DATE SQL : 2011 6 3
06-12 19:32:37.125: DEBUG/DEBUG(5777): (Actuel/Now) DATE : 3) 12/06/2011
06-12 19:32:37.125: DEBUG/DEBUG(5777): (Actuel/Now) Numero de semaine/Week number (annee/year) : 24
06-12 19:32:37.135: DEBUG/DEBUG(5777): (Passe/Past) DATE : 2) 03/07/2011
06-12 19:32:37.135: DEBUG/DEBUG(5777): (Passe/Past) Numero de semaine/Week number (annee/year) : 27
什麼,我做錯了任何想法?
感謝您的快速回復,我的代碼現在可以運行:) – DoesNotCompute 2011-06-12 18:38:30
基於月份的日期 - 天才/直覺/理性! – RunLoop 2013-12-16 16:30:49