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

7

-4

I got below code on stackoverflow which return total number of week in current year, but it is hardcoded which'll not work on 2014 and 2016. How I get total number of week in current year dynamically???

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2015);
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DAY_OF_MONTH, 31);

int ordinalDay = cal.get(Calendar.DAY_OF_YEAR);
int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1; // Sunday = 0
int numberOfWeeks = (ordinalDay - weekDay + 10) / 7;
System.out.println(numberOfWeeks);

I did like this just check it is current method?

     Calendar cal = Calendar.getInstance();
     int year = cal.get(Calendar.YEAR);
     cal.set(Calendar.YEAR, year);
     cal.set(Calendar.MONTH, Calendar.DECEMBER);
     cal.set(Calendar.DAY_OF_MONTH, 31);

     int ordinalDay = cal.get(Calendar.DAY_OF_YEAR);
     int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1; // Sunday = 0
     int numberOfWeeks = (ordinalDay - weekDay + 10) / 7;
     System.out.println(numberOfWeeks);
Vice answered 26/8, 2013 at 6:58 Comment(3)
how??? how i automaticlaly detect total numbe rof year programicallyVice
this code say dayis in month is 31 but most mnths have 30 days also febuary have 28 daysVice
i did like what u say just check above is this correct method??Vice
E
3

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;
    }
Endowment answered 26/8, 2013 at 7:5 Comment(8)
@Vice ...so use a variable? You do know how to do that?Sarcophagus
you can make it as variable. And this is very easy to convert that code into dynamic ways.. what is the problem with you?Endowment
i want to return total number of week in the year is i guess 52 weeks in year or somethingVice
i did like what u say just check above is this correct method??Vice
thnx for code actully i m create an application dont knowbaout next year how many weeks so can i trust ur code is this perfect?Vice
ur code return 5 which pankaj u written above dynamically is return 5Vice
There is a mistake in your code : december is month 11 and not 12: mCalendar.set(Calendar.MONTH, 11); is correct settingVrablik
@Jean-ChristopheBlanchard Yes you are correct. We should use Calendar.DECEMBER instead of 11. In my last code snippet I used that too. Now pointed blocks has been updated. Thank youEndowment
S
0

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.

Swagerty answered 26/8, 2013 at 7:4 Comment(4)
but it say months is 31? some month is 30 or 28 daysVice
what i write instead of this line cal.set(Calendar.YEAR, 2015);Vice
1.) 31 is the number of december days... 2.)cal.set(Calendar.YEAR, year);Swagerty
i did like what u say just check above is this correct method??Vice
P
0

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);            

}
Popup answered 27/8, 2013 at 20:58 Comment(0)
C
0

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.

Cytokinesis answered 18/7, 2014 at 9:18 Comment(0)
P
0

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);
Perfective answered 11/8, 2015 at 21:42 Comment(0)
F
0

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;
}
Fredenburg answered 7/6, 2017 at 9:39 Comment(0)
F
0

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.

Flutist answered 7/9, 2017 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.