How to get the total number of weeks in the current year?

大憨熊 提交于 2019-12-01 14:53:44

Use the below code

Calendar mCalendar = new GregorianCalendar(); 
mCalendar.set(Calendar.YEAR, 2014); // Set only year 
mCalendar.set(Calendar.MONTH, Calendar.DECEMBER); // Don't change
mCalendar.set(Calendar.DAY_OF_MONTH, 31); // Don't change
int totalWeeks = mCalendar.get(Calendar.WEEK_OF_YEAR);

Don't care about 30, 28 and 29 days of month. Last day of a year (Any year) is always 31 Dec. So you need to set that day. And mCalendar.get(Calendar.WEEK_OF_YEAR) will return the total weeks in that year.


Update for dynamic way

private int getTotalWeeksInYear(int year) {
        Calendar mCalendar = new GregorianCalendar(); 
        mCalendar.set(Calendar.YEAR, year); // Set only year 
        mCalendar.set(Calendar.MONTH, Calendar.DECEMBER); // Don't change
        mCalendar.set(Calendar.DAY_OF_MONTH, 31); // Don't change
        return mCalendar.get(Calendar.WEEK_OF_YEAR);
    }

    // Call as
    int totalWeeks = getTotalWeeksInYear(2014);

Looking for bug in above code. By the time you can use below code that is working fine

private int getTotalWeeksInYear(int year) {
        Calendar mCalendar = new GregorianCalendar(TimeZone.getDefault()); 
        mCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        // Workaround
        mCalendar.set(year, 
                Calendar.DECEMBER, 
                31);
        int totalDaysInYear = mCalendar.get(Calendar.DAY_OF_YEAR);
        System.out.println(totalDaysInYear);
        int totalWeeks = totalDaysInYear / 7; 
        return totalWeeks;
    }

just use current year in this method:

int year = Calendar.getInstance().get(Calendar.YEAR);
cal.set(Calendar.YEAR, year);

Leave rest the same as in your example.

I have a DatePickerFragment, its onDateSet function

public void onDateSet(DatePicker view, int year, int month, int day) {
    ((Main) getActivity()).setWeek(year, month, day);
} 

only returns year, month and day only. So I have to do the following to get the week.

public void getWeek(int year, int month, int day){

        final Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        int week = c.get(Calendar.WEEK_OF_YEAR);            

}

You can use something like

private Calendar getCalendar(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance(Locale.GERMAN);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(year, month, day);
    return calendar;
}

private int getTotalWeeksInYear(int year) {
    return getCalendar(year, Calendar.DECEMBER, 31).get(Calendar.WEEK_OF_YEAR); 
}

Remember that definition of Week of Year is local dependent. It means that in different locales returned value of get(Calendar.WEEK_OF_YEAR) will be different. For example in Locale.GERMAN according to DIN 1355-1 / ISO 8601 the first Week of Year is the week contains 4 or more days in the new year. For the Locale.US the first Week of Year is the week where the 1 January belongs to.

the fastest way is: Very important to set properly the Calendar depending from where are you from, because of numbering of weeks is determined to given standards. For example in Europ is ISO 8061. It means that 1 week of the year beginn when 01.01.RRRR is Thursday or further, and week beginns from Monday.

GregorianCalendar gregorianCalendar = new GregorianCalendar(year,12,31);
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY); // week beginn from Monday
gregorianCalendar.setMinimalDaysInFirstWeek(4); // 1week mininum from Thursday
int totalWeeks = gregorianCalendar.getMaximum(Calendar.WEEK_OF_YEAR);

Why not to use getActualMaximum property of Calendar ?

private int getWeeksCount(final int year, final Locale locale)
{
    Calendar calendar = Calendar.getInstance(locale);
    calendar.set(Calendar.YEAR, year);

    int totalWeekNumber = calendar.getActualMaximum(Calendar.WEEK_OF_YEAR);
    return totalWeekNumber;
}

You can use the Calendar.getActualMaximum(int) method to get the total number of weeks in the current year as follows:

int totalNumberOfWeeks = Calendar.getInstance().getActualMaximum(Calendar.WEEK_OF_YEAR);

Simple.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!