如何获得当年的总周数?

时间:2013-08-26 06:58:31

标签: java android android-calendar

我在stackoverflow上获得了下面的代码,它返回当前年份的总周数,但它是硬编码的,在2014年和2016年不起作用。我如何动态获得当年的总周数???

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

我这样做只是检查它是当前的方法吗?

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

7 个答案:

答案 0 :(得分:3)

使用以下代码

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

不关心一个月的30,28和29天。一年的最后一天(任何一年)总是在12月31日。所以你需要设定那一天。 mCalendar.get(Calendar.WEEK_OF_YEAR)将返回该年的总周数。


动态方式更新

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

在上面的代码中查找错误。当你可以使用下面的代码工作正常

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

答案 1 :(得分:0)

在这种方法中使用当前年份:

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

保持休息与示例相同。

答案 2 :(得分:0)

我有一个DatePickerFragment,它的onDateSet函数

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

仅返回年,月和日。所以我必须做以下事情来获得这一周。

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

}

答案 3 :(得分:0)

您可以使用类似

的内容
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); 
}

请记住,每周的定义取决于当地情况。这意味着在不同的语言环境中,get(Calendar.WEEK_OF_YEAR)的返回值会有所不同。 例如,在根据DIN 1355-1 / ISO 8601的Locale.GERMAN中,第一周是新年中包含4天或更多天的一周。对于Locale.US,第一周是1月1日所属的一周。

答案 4 :(得分:0)

最快的方法是: 非常重要的是根据您的来源正确设置日历,因为数周的数量是根据给定的标准确定的。例如,在Europ中是ISO 8061.这意味着一年中的1周开始时01.01.RRRR是星期四或更远,而星期从星期一开始。

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

答案 5 :(得分:0)

为什么不使用<?php echo WP_HOME; ?>/wp-content/uploads/image.jpg 的{​​{1}}属性?

getActualMaximum

答案 6 :(得分:0)

您可以使用Calendar.getActualMaximum(int)方法获取当前年份的总周数,如下所示:

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

简单。