Eng*_*uad 75 java date simpledateformat
我想知道为什么'Y'会回归2012而'y'会在2011年回归SimpleDateFormat:
System.out.println(new SimpleDateFormat("Y").format(new Date())); // prints 2012
System.out.println(new SimpleDateFormat("y").format(new Date())); // prints 2011
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释原因吗?
Ara*_*ram 80
一年和一年.来自javadoc
一周的年份与WEEK_OF_YEAR周期同步.第一周和最后一周(包括)之间的所有周都具有相同的周年值.因此,一周的第一天和最后一天可能具有不同的日历年值.
例如,1998年1月1日是星期四.如果getFirstDayOfWeek()是MONDAY并且getMinimalDaysInFirstWeek()是4(ISO 8601标准兼容设置),那么1998年第1周将于1997年12月29日开始,并于1998年1月4日结束.过去三天的一周是1998年但是,如果getFirstDayOfWeek()是SUNDAY,则1998年第1周从1998年1月4日开始,到1998年1月10日结束; 1998年的前三天是1997年第53周的一部分,其一周是1997年.
Bri*_*ice 11
这是一个带有一些代码的Java 8更新,因为GregorianCalendar可能会被弃用或从未来的JDK版本中删除.
新代码在WeekFields类中处理,特别是对于具有字段访问器的小写y/大写.YweekBasedYear()
返回一个字段,以根据此WeekFields访问基于周的年份的年份.这代表了一周的概念,其中周开始于固定的星期几,例如星期一,每周属于一年.此字段通常与dayOfWeek()和weekOfWeekBasedYear()一起使用.
第一周(1)是从getFirstDayOfWeek()开始的一周,其中一年中至少有getMinimalDaysInFirstWeek()天.因此,第一周可以在年初之前开始.如果第一周在年初之后开始,则之前的时段是在上一年的最后一周.
该字段可用于任何日历系统.
在解析的解析阶段,可以从基于周的年,周和星期创建日期.
在严格模式下,所有三个字段都根据其有效值范围进行验证.验证了周年字段,以确保生成的基于周的年份是所请求的基于周的年份.
在智能模式下,所有三个字段都根据其有效值范围进行验证.基于星期的年份字段的验证范围为1到53,这意味着生成的日期可以是指定的下一周的年份.
在宽松模式下,年份和星期几根据有效值范围进行验证.计算得出的日期相当于以下三阶段方法.首先,在请求的基于周的年份的第一周的第一天创建日期.然后,以周为基础的年份,减去一年,并将该数量添加到该日期的几周内.最后,调整到本地周内正确的星期几.
此WeekFields实例的设置取决于区域设置,并且可能根据它设置不同的设置,美国和欧洲国家(如法国)可能会有不同的一天作为一周的开始.
例如DateFormatterBuilder,Java 8,使用语言环境实例化解析器,并使用此语言环境作为Y符号:
public final class DateTimeFormatterBuilder {
...
private void parsePattern(String pattern) {
...
} else if (cur == 'Y') {
// Fields defined by Locale
appendInternal(new WeekBasedFieldPrinterParser(cur, count));
} else {
...
static final class WeekBasedFieldPrinterParser implements DateTimePrinterParser {
...
/**
* Gets the printerParser to use based on the field and the locale.
*
* @param locale the locale to use, not null
* @return the formatter, not null
* @throws IllegalArgumentException if the formatter cannot be found
*/
private DateTimePrinterParser printerParser(Locale locale) {
WeekFields weekDef = WeekFields.of(locale);
TemporalField field = null;
switch (chr) {
case 'Y':
field = weekDef.weekBasedYear();
if (count == 2) {
return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, 0);
} else {
return new NumberPrinterParser(field, count, 19,
(count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, -1);
}
case 'e':
case 'c':
field = weekDef.dayOfWeek();
break;
case 'w':
field = weekDef.weekOfWeekBasedYear();
break;
case 'W':
field = weekDef.weekOfMonth();
break;
default:
throw new IllegalStateException("unreachable");
}
return new NumberPrinterParser(field, (count == 2 ? 2 : 1), 2, SignStyle.NOT_NEGATIVE);
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
这是一些例子
System.out.format("Conundrum : %s%n",
ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC"))
.format(DateTimeFormatter.ofPattern("YYYYMMdd'T'HHmms'S'")));
System.out.format("Solution : %s%n",
ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC"))
.format(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmms'S'")));
System.out.format("JVM Locale first day of week : %s%n",
WeekFields.of(Locale.getDefault()).getFirstDayOfWeek());
System.out.format("US first day of week : %s%n",
WeekFields.of(Locale.US).getFirstDayOfWeek());
System.out.format("France first day of week : %s%n",
WeekFields.of(Locale.FRANCE).getFirstDayOfWeek());
System.out.format("JVM Locale min days in 1st week : %s%n",
WeekFields.of(Locale.getDefault()).getMinimalDaysInFirstWeek());
System.out.format("US min days in 1st week : %s%n",
WeekFields.of(Locale.US).getMinimalDaysInFirstWeek());
System.out.format("JVM Locale min days in 1st week : %s%n",
WeekFields.of(Locale.FRANCE).getMinimalDaysInFirstWeek());
System.out.format("JVM Locale week based year (big Y): %s%n",
ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")).get(WeekFields.of(Locale.FRANCE).weekBasedYear()));
System.out.format("France week based year (big Y) : %s%n",
ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")).get(WeekFields.of(Locale.FRANCE).weekBasedYear()));
System.out.format("US week based year (big Y) : %s%n",
ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")).get(WeekFields.of(Locale.US).weekBasedYear()));
Run Code Online (Sandbox Code Playgroud)
而在现场和上情况方面Y,你可以使用命令行选项打-Duser.language=(fr,en,es等),或强制在调用时的语言环境:
System.out.format("English localized : %s%n",
ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC"))
.format(DateTimeFormatter.ofPattern("YYYYMMdd'T'HHmms'S'", Locale.ENGLISH)));
System.out.format("French localized : %s%n",
ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC"))
.format(DateTimeFormatter.ofPattern("YYYYMMdd'T'HHmms'S'", Locale.FRENCH)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22769 次 |
| 最近记录: |