0

Why does calendar.WEEK_OF_YEAR prints 3 instead of 6.

GregorianCalendar calendar = new GregorianCalendar();
System.out.println("time"+calendar.getTime());
System.out.println("week of year "+calendar.WEEK_OF_YEAR);

Output

time Tue Feb 09 12:58:02 IST 2021
week of year 3
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
saurabh kumar
  • 155
  • 5
  • 26

3 Answers3

3

tl;dr

LocalDate.now().get( IsoFields.WEEK_OF_WEEK_BASED_YEAR )

February 9, 2021 is in week # 6 of the ISO 8601 week-based year of 2021 that runs from Monday January 4, 2021 through Sunday January 2, 2022 (inclusive).

In standard notation, Feb 9, 2021 is the second day Tuesday of the sixth week of the week-based year 2021:
2021-W06-2

Details

The terrible class GregorianCalendar was supplanted years ago by the modern java.time classes defined in JSR 310.

java.time

Get current date.

LocalDate today = LocalDate.now( ZoneId.of( "Asia/Kolkata" ) ) ;

Get the week of a week-based year, using the definition of ISO 8601. In this definition:

  • a week runs Monday-Sunday, and
  • week # 1 contains the first Thursday of the calendar-year.

As a consequence of that definition, each week-based year has either 52 or 53 whole weeks.

int week = today.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );

And get the year number of the week-based year for that date. This may differ from the calendar year number in the first and last weeks.

int weekYear = now.get ( IsoFields.WEEK_BASED_YEAR );

For example, the ISO 8601 week-based year for 2021 started January 4, 2021. February 9, 2021 is in week # 6.

See this list of weeks in ISO 8601 week-based year of 2021.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

calendar.WEEK_OF_YEAR is a constant, you don't use it like that.

You probably meant calendar.get(Calendar.WEEK_OF_YEAR) (and, today February the 9th it will print 7, not 6, because it's 1-based, not 0-based).

Note that, as the nice answer from Basil Bourque explains, you should use the new DateTime API.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
2

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

The answer by Basil Bourque nicely explains how to use the Java SE 8 date-time API. If you still want to use the legacy API, you have a couple of options:

import java.text.SimpleDateFormat;
import java.util.Calendar;

class Main {
    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();

        // Getting current week using SimpleDateFormat
        System.out.println("Current week: " + new SimpleDateFormat("w").format(calendar.getTime()));

        // Getting current week using Calendar.WEEK_OF_YEAR
        System.out.println("Current week: " + calendar.get(Calendar.WEEK_OF_YEAR));
    }
}

Output:

Current week: 6
Current week: 6

The ISO 8601 definition for week 01 is the week with the first Thursday of the Gregorian year (i.e. of January) in it. With this definition, week 01 in 2021 starts on 4th January and therefore the current week is the 6th week.

enter image description here

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110