如何获得当年的总周数? [英] How to get the total number of weeks in the current year?

查看:227
本文介绍了如何获得当年的总周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

使用以下代码

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)将返回该年的总周数。

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

这篇关于如何获得当年的总周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆