计算给定年份的周数 [英] calculate number of weeks in a given year

查看:113
本文介绍了计算给定年份的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到任何一年的周数。即使 52 被接受为全球通用答案, 2015 2020 2026 实际上有 53 周。


$ b

解决方案

有没有什么办法可以计算这个,关于 ISO周日日期格式的维基百科文章,您可以使用以下代码计算。

 日历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 would like to get the number of weeks in any given year. Even though 52 is accepted as a generalised worldwide answer, the calendars for 2015, 2020 and 2026 actually have 53 weeks.

Is there any way that I can calculate this, or any functions that will help me out?

解决方案

According to the wikipedia article on ISO week date format, You can calculate it using following code.

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

这篇关于计算给定年份的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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