Java Utility Library

Java GregorianCalendar - getWeekYear() Method



The java.util.GregorianCalendar.getWeekYear() method returns the week year represented by this GregorianCalendar.

Syntax

public int getWeekYear()

Parameters

No parameter is required.

Return Value

Returns the week year represented by this GregorianCalendar. If the ERA value is BC, the year is represented by 0 or a negative number: BC 1 is 0, BC 2 is -1, BC 3 is -2, and so on.

Exception

Throws IllegalArgumentException, if any of the calendar fields is invalid in non-lenient mode.

Example:

In the example below, the java.util.GregorianCalendar.getWeekYear() method returns the week year represented by the given GregorianCalendar object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar object with specified date
    GregorianCalendar Cal = new GregorianCalendar(2015, 10, 25);

    //printing the Calendar
    System.out.println("The Calendar is: " + Cal.getTime());

    //printing the week year of the specified Calendar
    int weekyear = Cal.getWeekYear();
    System.out.println("Week Year: " + weekyear);    

    //printing the number of weeks in the specified Calendar
    int week = Cal.getWeeksInWeekYear();
    System.out.println("Number of weeks: " + week);
  }
}

The output of the above code will be:

The Calendar is: Wed Nov 25 00:00:00 UTC 2015
Week Year: 2015
Number of weeks: 52

❮ Java.util - GregorianCalendar