Calendar.getWeeksInWeekYear()
Calendar.getWeeksInWeekYear() Number of weeks in the year .
Returns the number of weeks .
Getting current year ( number of weeks )
package my_proj;
import java.util.Calendar;
public class my_first {
public static void main(String[] args) {
Calendar my_cal = Calendar.getInstance();// created calendar
System.out.println(my_cal.getWeeksInWeekYear());//52
}
}
For any input year ( Number of weeks )
package my_proj;
import java.util.Calendar;
public class my_first {
public static void main(String[] args) {
Calendar my_cal = Calendar.getInstance();// created calendar
my_cal.set(2016, 1, 25); // Year, month, date
System.out.println(my_cal.getWeeksInWeekYear());//53
}
}
Using for loop we can check years in a range
package my_proj;
import java.util.Calendar;
public class my_first {
public static void main(String[] args) {
Calendar my_cal = Calendar.getInstance();// created calendar
my_cal.set(2016, 1, 25); // Year, month, date
System.out.println(my_cal.getWeeksInWeekYear());//53
for (int i=2000; i<2030; i++) {
System.out.println(i);
my_cal.set(i, 1, 25); // Year, month, date
System.out.println(my_cal.getWeeksInWeekYear());//53
}
}
}
We will get number of weeks as 53 for the years 2000, 2005,2011, 2016,2022 & 2028, for other years output will be 52.
By changing first day of week by using setFirstDayOfWeek() we can change the output of getWeeksInWeekYear()
« getLeastMaximum() « getActualMaximum()
« Date & time tutorials
« Java
This article is written by plus2net.com team.
plus2net.com