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开发者_StackOverflow中文版: 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?
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.
Month is 0 based in java (specially Date related classes)
Here is api doc for Calendar::set
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH
. Previous values of other calendar fields are retained. If this is not desired, call clear()
first.
Parameters:
year-> the value used to set the YEAR
calendar field.
month -> the value used to set the MONTH
calendar field. Month value is 0-based. e.g., 0 for January.
date -> the value used to set the DAY_OF_MONTH
calendar field.
Did you check the actual value of Integer.parseInt(tokens[1])? There's nothing seems to be wrong with your code, please check the values of you are getting from sqlite db ?
精彩评论