为什么 2010 年 12 月 31 日返回 1 作为一年中的一周? [英] Why dec 31 2010 returns 1 as week of year?

查看:29
本文介绍了为什么 2010 年 12 月 31 日返回 1 作为一年中的一周?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

Calendar c = Calendar.getInstance();
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime( sdf.parse("31/12/2010"));
out.println( c.get( Calendar.WEEK_OF_YEAR ) );  

打印 1

Joda 时间也是如此.

Same happens with Joda time.

:)

推荐答案

一周的定义是 Locale 依赖.

它在美国的定义在其他帖子中讨论.例如在德国 (DIN 1355-1/ISO 8601):一年中的第一周*是新年中有 4 天或更多天的第一周.

How it is defined in US is discused in the other posts. For example in Germany (DIN 1355-1 / ISO 8601): the first Week* of Year is the first week with 4 or more days in the new year.

*一周的第一天是星期一,最后一天是星期天

Java 的 Calendar 很注重语言环境.例如:

And Java’s Calendar pays attention to the locale. For example:

public static void main(String[] args) throws ParseException {

    DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date lastDec2010 = sdf.parse("31/12/2010");

    Calendar calUs = Calendar.getInstance(Locale.US);       
    calUs.setTime(lastDec2010);

    Calendar calDe = Calendar.getInstance(Locale.GERMAN);       
    calDe.setTime(lastDec2010);

    System.out.println( "us: " + calUs.get( Calendar.WEEK_OF_YEAR ) ); 
    System.out.println( "de: " + calDe.get( Calendar.WEEK_OF_YEAR ) );
}

印刷品:

us: 1
de: 52

添加对于美国(我认为墨西哥也是如此),一年中的 1. 周是 1. 一月所属的一周.-- 因此,如果 1. Januar 是星期六,则之前的星期五(12 月 31 日)属于同一周,在这种情况下,这一天属于 2011 年的 1. 周.

ADDED For the US (and I can think of that it is the same for Mexico) the 1. Week of Year is the week where the 1. January belongs to. -- So if 1. Januar is a Saturday, then the Friday before (31. Dec) belongs the same week, and in this case this day belongs to the 1. Week of Year 2011.

这篇关于为什么 2010 年 12 月 31 日返回 1 作为一年中的一周?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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