javaandroiddatecalendarweek-number

Android Calendar returning the wrong month number


I'm working on a function to compare the week number of some sqlite stored dates with the week number of the current date.

But the set(y, m, d) method of the Calendar class always returns a date with a wrong month number in my case it return a date with the month set to july instead of june.

Here's my snippet of code :

//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));

The output of the debugger (dates are French formated)

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

Any idea of what I did wrong?


Solution

  • It's because the month numbers taken by the set() method are zero-indexed (eg JANUARY = 0), but you're passing in the normal month number (ie January = 1).

    I suggest using the provided SimpleDateFormat.parse() method.