1

When I execute the below piece of code I'm getting same output.

public static void main(String[] args) throws ParseException {    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date procDate = sdf.parse("2016-01-01");
    Calendar cal = Calendar.getInstance(Locale.UK);
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setTime(procDate);
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) ); 
    System.out.println(cal.get(Calendar.YEAR) );

    procDate = sdf.parse("2016-12-27");
    cal.setTime(procDate);
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) ); 
    System.out.println(cal.get(Calendar.YEAR) );            
}

Actual output:

52
2016
52
2016

Expected Output:

52
2015
52
2016

Or

0
2016
52
2016

Locale and setFirstDayOfWeek are i can't change.

Tom
  • 16,842
  • 17
  • 45
  • 54
Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54
  • See Questions: [*android how to identify the same days of a week*](https://stackoverflow.com/q/32298959/642706) – and – [*Why dec 31 2010 returns 1 as week of year?*](https://stackoverflow.com/q/4608470/642706) – and – [*Understanding java.util.Calendar WEEK_OF_YEAR*](https://stackoverflow.com/q/10893443/642706) – and – [*Is there any DateTime library which supports quarter of the year and week of the year Java?*](https://stackoverflow.com/q/38571453/642706) – Basil Bourque Feb 19 '18 at 01:34

4 Answers4

0

Let me quote from the source:

/**
 * Field number for <code>get</code> and <code>set</code> indicating the
 * week number within the current year.  The first week of the year, as
 * defined by <code>getFirstDayOfWeek()</code> and
 * <code>getMinimalDaysInFirstWeek()</code>, has value 1.  Subclasses define
 * the value of <code>WEEK_OF_YEAR</code> for days before the first week of
 * the year.
 *
 * @see #getFirstDayOfWeek
 * @see #getMinimalDaysInFirstWeek
 */

Let us have a look at this experiment:

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date procDate = sdf.parse("2016-01-01");
    Calendar cal = Calendar.getInstance(Locale.UK);
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setTime(procDate);
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
    System.out.println(cal.get(Calendar.YEAR) );
    cal.add(Calendar.DAY_OF_YEAR, 1);
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
    System.out.println(cal.get(Calendar.YEAR) );
    cal.add(Calendar.DAY_OF_YEAR, 1);
    System.out.println(cal.get(Calendar.DAY_OF_YEAR));
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
    System.out.println(cal.get(Calendar.YEAR) );

    procDate = sdf.parse("2016-12-27");
    cal.setTime(procDate);
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
    System.out.println(cal.get(Calendar.YEAR) );
}

52
2016
52
2016
3
1
2016
52
2016

Conclusion:

WEEK_OF_YEAR is set to 1 on the first day of the full week in year. Before this day, it depends on implementation. In default Java implementation it is 52 by default. You can set cal.setMinimalDaysInFirstWeek(1); and then it should fix your case.

Why? Ask guys from Sun.

Community
  • 1
  • 1
xenteros
  • 15,586
  • 12
  • 56
  • 91
0

The Calendar class doesn't know that your calls of

cal.get(Calendar.WEEK_OF_YEAR); 
cal.get(Calendar.YEAR); 

are supposed to be in a relationship together. Since beeing in the last week of year X can mean you are already in year X+1, what you see as output is actually correct.

To archieve what you want you should just use another DateFormater to print out the week of the year together with the corresponding year:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("w Y");
Date procDate = sdf.parse("2016-01-01");
System.out.println(sdf2.format(procDate));

procDate = sdf.parse("2016-12-27");
System.out.println(sdf2.format(procDate));

Result:

53 2015
52 2016
OH GOD SPIDERS
  • 3,091
  • 2
  • 13
  • 16
0

See this answer

Understanding java.util.Calendar WEEK_OF_YEAR

The problema is related to this unconfortable use of week of the year by java. Anyway, calling cal.setMinimalDaysInFirstWeek(3); will fix the result (in terms of difference. In my case January stil gives me week 53 (!)

Community
  • 1
  • 1
Massimo Petrus
  • 1,881
  • 2
  • 13
  • 26
0

Move back to starting day of the week I'm getting expected result. In my case Sunday is the starting day of week. So i setted cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY), this resolved my problem.

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date procDate = sdf.parse("2016-01-01");
    Calendar cal = Calendar.getInstance(Locale.UK);
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setTime(procDate);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //Starting day of week
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) ); 
    System.out.println(cal.get(Calendar.YEAR) );

    procDate = sdf.parse("2016-12-27");
    cal.setTime(procDate);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //Starting day of week
    System.out.println(cal.get(Calendar.WEEK_OF_YEAR) ); 
    System.out.println(cal.get(Calendar.YEAR) );            
}

Output:

52
2015
52
2016

Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54