Samantha Withanage
Samantha Withanage

Reputation: 3851

android java, week number of a date is incorrect

in my android app I'm working on to get the week number of a provided date. but it doesn't give me the correct week number I'm using following code to get the week number.

String format = "dd-MM-yyyy";
SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);

but this not give me what I'm expecting. for instance lets take January 2016. enter image description here

As of January 2016 as seen from the above calendar, days belongs to the week number 1 are only 1st,2nd and 3rd. and for the week number 2, relevant days are 4th, 5th, 6th, 7th,8th, 9th and 10th. then for the week number 3, relevant days are 11th, 12th, 13th, 14th, 15th, 16th and 17th and so on. but for the date 12/01/2016 above code gives me week number 2 instead of week number 3. am I doing something wrong or is this the way the function works normally. how can I meet my goal to get the week number as I mention above.

Upvotes: 0

Views: 1665

Answers (2)

1615903
1615903

Reputation: 34733

1st, 2nd and 3rd of January are actually week 53 of year 2015. The code is working correctly, your assumptions about week numbering are wrong. The default behaviour of GregorianCalendar is the same as ISO definition for the first week in Wikipedia:

The ISO 8601 definition for week 01 is the week with the year's first Thursday in it. The following definitions based on properties of this week are mutually equivalent, since the ISO week starts with Monday:

  • It is the first week with a majority (4 or more) of its days in January.
  • Its first day is the Monday nearest to 1 January.
  • It has 4 January in it. Hence the earliest possible dates are 29 December through 4 January, the latest 4 through 10 January.
  • It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days.

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, it is part of week 53 of the previous year; if on a Saturday, it is part of week 52 (or 53 if the previous year was a leap year); if on a Sunday, it is part of week 52 of the previous year.

Upvotes: 5

Joren
Joren

Reputation: 236

Found at https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html :

Values calculated for the WEEK_OF_YEAR field range from 1 to 53. The first week of a calendar year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1.

So your first week will be week 0 because it starts counting from your first full 7 days week. You can change this by setting getMinimalDaysInFirstWeek() to a lower value

Upvotes: 3

Related Questions