Java SimpleDateFormat将日期移动一年 [英] Java SimpleDateFormat shifts Date by one year

查看:233
本文介绍了Java SimpleDateFormat将日期移动一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的结果非常奇怪,我无法理解。

I'm getting very weird results, which I can not understand.

public class Test {

  private static DateFormat df = new SimpleDateFormat("dd.MM.YYYY HH:mm");

  public static void main(String[] args) {
    Date d = new Date(1356912000000L);
    System.out.println(d);
    System.out.println(df.format(d));
  }  
}

给出输出:

Mon Dec 31 01:00:00 CET 2012
31.12.2013 01:00

我认为这可能与语言环境有关,但这是整整一年的转变!谁能解释为什么这样做?

I assume that this might be some issues with locales, but that's a shift by a whole year ! May anyone explain why it performs this way ?

推荐答案

YYYY 是周年,而不是日历年。您需要 yyyy 。以下是Java的相关详细信息:

YYYY is the week-year, not the calendar year. You want yyyy instead. Here's Java's relevant details:


为WEEK_OF_YEAR字段计算的值范围为1到53。
日历年的第一周是最早的7天时段
(从getFirstDayOfWeek()开始),该日期至少包含该年的
getMinimalDaysInFirstWeek()天。因此,它取决于
1月1日星期几的值getMinimalDaysInFirstWeek(),getFirstDayOfWeek()和
的值。一年的第1周到
的以下第1周之间的周年份(不包括年份)从2到52或53依次编号为
(参与
朱利安-格里高利安过渡的年份除外)。

Values calculated for the WEEK_OF_YEAR field range from 1 to 53. The first week of a calendar year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year (exclusive) are numbered sequentially from 2 to 52 or 53 (except for year(s) involved in the Julian-Gregorian transition).

getFirstDayOfWeek ()和getMinimalDaysInFirstWeek()值是在构造
GregorianCalendar时使用与语言环境相关的资源初始化的
。当getFirstDayOfWeek()为MONDAY且
getMinimalDaysInFirstWeek()为4时,星期确定与ISO
8601标准兼容,该值在首选标准的语言环境
中使用。这些值可以通过
调用setFirstDayOfWeek()和setMinimalDaysInFirstWeek()来明确设置。

The getFirstDayOfWeek() and getMinimalDaysInFirstWeek() values are initialized using locale-dependent resources when constructing a GregorianCalendar. The week determination is compatible with the ISO 8601 standard when getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4, which values are used in locales where the standard is preferred. These values can explicitly be set by calling setFirstDayOfWeek() and setMinimalDaysInFirstWeek().

一周的一年与WEEK_OF_YEAR周期同步。
之间的所有第一周和最后一周(包括最后一周)都具有相同的周年值。
因此,一周的第一天和最后一天的日历年值可能会不同。

A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.

例如,1998年1月1日是星期四。如果getFirstDayOfWeek()为星期一
,而getMinimalDaysInFirstWeek()为4(ISO 8601标准
兼容设置),则1998年的第1周开始于1997年12月29日,即
并于1月4日结束。 1998。星期是1997日历年的最后三个
天。但是,如果getFirstDayOfWeek()是
SUNDAY,则1998年的第1周从1998年1月4日开始,到
1998年1月10日; 1998年的前三天是1997年第53周
的一部分,而一周是1997年。

For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997.

这篇关于Java SimpleDateFormat将日期移动一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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