Java如何“周年"真的有效吗? [英] How does Java "week year" really work?

查看:32
本文介绍了Java如何“周年"真的有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这开始是一个简单的错误:我的格式字符串中有 YYYY 而不是 yyyy 用于 SimpleDateFormat 对象.但是我对格式字符串不正确的测试结果感到非常困惑.

This started as a simple error: I had YYYY instead of yyyy in my format string for a SimpleDateFormat object. But I'm totally baffled by the results of my tests with the incorrect format string.

此代码:

@Test
public void whatTheHell() {
        try {
                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");

                Date d1 = sdf.parse("01/07/2016");
                Date d2 = sdf.parse("02/08/2016");
                Date d3 = sdf.parse("11/29/2027");

                System.out.println(d1.toString());
                System.out.println(d2.toString());
                System.out.println(d3.toString());
        } catch (ParseException pe) {
                fail("ParseException: " + pe.getMessage());
        }
}

产生这个输出:

Sun Dec 27 00:00:00 PST 2015
Sun Dec 27 00:00:00 PST 2015
Sun Dec 27 00:00:00 PST 2026

我在这里阅读了有关Y"参数的文档:https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html,但我仍然看不到在这里工作的逻辑.特别是最后一个例子:我可以有点理解一月(也许二月)的日期如何翻译成前一年的十二月,但是将 11 月 29 日的日期向后移动 11 个月让我感到困惑.12 月 27 日有什么特别之处?

I've read the documentation on the 'Y' parameter here: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html, but I still can't see the logic that's working here. Particularly the last instance: I can kinda-sorta understand how the dates in January (& maybe February) can be translated into December of the previous year, but moving the date of November 29th backwards by 11 months baffles me. And what's so special about December 27th?

谁能解释一下?

更多信息

@Jan 建议依赖 toString() 方法可能是一个问题,所以我定义了一个日期格式来打印 YYYY MM dd '-' yyyy MM dd 与上面相同的代码.这是额外的输出:

@Jan suggested that relying on the toString() method could be a problem, so I defined a date format to print YYYY MM dd '-' yyyy MM dd in the same code as above. Here is the additional output:

2016 12 27 - 2015 12 27
2016 12 27 - 2015 12 27
2027 12 27 - 2026 12 27

推荐答案

很简单:2015 年 12 月 27 日是 2016 年第 1 周的第 1 天(而 2026 年 12 月 27 日是 2027 年第 1 周的第 1 天)).这可以通过添加这些行来验证:

It's simple: December 27 2015 is day 1 of week 1 of week-year 2016 (and December 27 2026 is day 1 of week 1 of week-year 2027). This can be verified by adding these lines:

SimpleDateFormat odf = new SimpleDateFormat("YYYY-ww-u");
System.out.println(odf.format(d1));
System.out.println(odf.format(d2));
System.out.println(odf.format(d3));

如果 SimpleDateFormat 输出一个日期,它可以使用所有字段:年、月、日、星期几、一个月中的一周、一年中的一周、一周年等.

If a SimpleDateFormat outputs a date it can use all fields: year, month, day, day of week, week of month, week in year, week-year etc.

在解析时,SimpleDateFormat 需要一组匹配的值:日、月、年 星期几、一年中的一周、一周年.由于您提供了一周年但没有提供一周中的哪一天和一年中的一周,因此这些值已假定为 1.

On parsing, SimpleDateFormat expects a matching set of values: either day, month, year or day of week, week in year, week-year. Since you supplied a week-year but did not supply day of week and week in year, those to values have been assumed as 1.

实际值取决于您的语言环境:

The actual values depend on your locale:

  • 一年中的哪一周是第 1 周
  • 哪一天是一周的第一天

(参见 https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#week_and_year)

在我的系统上(使用 de-ch 语言环境,格式为EEE MMM dd HH:mm:ss zzz yyyy - YYYY-ww-u")我得到

On my system (using de-ch locale, with "EEE MMM dd HH:mm:ss zzz yyyy - YYYY-ww-u" as format) I get

Mo Jan 04 00:00:00 MEZ 2016 - 2016-01-1
Mo Jan 04 00:00:00 MEZ 2016 - 2016-01-1
Mo Jan 04 00:00:00 MEZ 2027 - 2027-01-1

这篇关于Java如何“周年"真的有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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