Joda-Time 和 Joda-Time 的方式有所不同java.time
解释模式e
.
在乔达时代,e
图案指定星期几的数值 http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html:
Symbol Meaning Presentation Examples
------ ----------- ------------ -------
e day of week number 2
所以,使用e
相当于从日期对象获取星期几:
// using org.joda.time.DateTime and org.joda.time.format.DateTimeFormat
DateTime d = new DateTime(2016, 12, 21, 20, 50, 25, 0, DateTimeZone.UTC);
DateTimeFormatter fmt = DateTimeFormat.forPattern("e").withLocale(Locale.ENGLISH);
System.out.println(d.toString(fmt)); // 3
System.out.println(d.getDayOfWeek()); // 3
System.out.println(d.dayOfWeek().getAsText(Locale.ENGLISH)); // Wednesday
请注意,格式化程序和getDayOfWeek()
return 3
. The getDayOfWeek() method http://www.joda.org/joda-time/apidocs/org/joda/time/base/AbstractDateTime.html#getDayOfWeek--返回定义在中的值DateTimeConstants class http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeConstants.html, and 星期三的值为3 http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeConstants.html#WEDNESDAY (一周中的第三天 https://www.timeanddate.com/calendar/days/wednesday.html根据ISO的定义 https://www.timeanddate.com/calendar/days/).
In java.time
API,模式e
有不同的意义 https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#appendPattern-java.lang.String-:
Pattern Count Equivalent builder methods
------- ----- --------------------------
e 1 append special localized WeekFields element for numeric day-of-week
它使用本地化的WeekFields https://docs.oracle.com/javase/8/docs/api/java/time/temporal/WeekFields.html元素,这可能会根据区域设置而有所不同。与getDayOfWeek()
method:
ZonedDateTime z = ZonedDateTime.of(2016, 12, 21, 20, 50, 25, 0, ZoneOffset.UTC);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("e", Locale.ENGLISH);
System.out.println(z.format(fmt)); // 4
System.out.println(z.getDayOfWeek()); // WEDNESDAY
System.out.println(z.getDayOfWeek().getValue()); // 3
请注意,格式化程序使用英语区域设置的本地化星期几,其值为4
, 打电话时getDayOfWeek().getValue()
回报3
.
那是因为e
使用英语语言环境相当于使用java.time.temporal.WeekFields
:
// using localized fields
WeekFields wf = WeekFields.of(Locale.ENGLISH);
System.out.println(z.get(wf.dayOfWeek())); // 4
While getDayOfWeek()
相当于使用 ISO 的定义:
// same as getDayOfWeek()
System.out.println(z.get(WeekFields.ISO.dayOfWeek())); // 3
这是因为 ISO 的定义使用星期一作为一周的第一天,而WeekFields
英语语言环境使用星期日:
// comparing the first day of week
System.out.println(WeekFields.ISO.getFirstDayOfWeek()); // MONDAY
System.out.println(wf.getFirstDayOfWeek()); // SUNDAY
So the e
模式可能表现不同或不同getDayOfWeek()
,根据格式化程序中设置的区域设置(或 JVM 默认区域设置,如果未设置)。例如,在法语语言环境中,它的行为与 ISO 类似,而在某些阿拉伯语语言环境中,一周的第一天是星期六:
WeekFields.of(Locale.FRENCH).getFirstDayOfWeek(); // MONDAY
WeekFields.of(new Locale("ar", "AE")).getFirstDayOfWeek(); // SATURDAY
根据javadoc https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#appendPattern-java.lang.String-,唯一返回星期几的数值的模式似乎是本地化的模式。因此,解析输入2016-12-21 20:50:25 Wednesday December +0000 3
,你可以使用java.time.format.DateTimeFormatterBuilder
并加入日期/时间模式java.time.temporal.ChronoField
指示星期几的数值(ISO 非区域设置敏感字段):
String input = "2016-12-21 20:50:25 Wednesday December +0000 3";
DateTimeFormatter parser = new DateTimeFormatterBuilder()
// date/time pattern
.appendPattern("yyyy-MM-dd HH:mm:ss EEEE MMMM ZZ ")
// numeric day of week
.appendValue(ChronoField.DAY_OF_WEEK)
// create formatter with English locale
.toFormatter(Locale.ENGLISH);
ZonedDateTime date = ZonedDateTime.parse(input, parser);
另请注意,您不需要引用-
, :
和空格字符,因此模式变得更加清晰易读(IMO)。
我还设置了英语语言环境,因为如果不设置,它将使用 JVM 默认语言环境,并且不能保证始终为英语。即使在运行时,它也可以在没有通知的情况下进行更改,因此最好指定一个,特别是如果您已经知道输入是什么语言的话。
Update: 大概是ccccc
模式应该有效,因为它相当于appendText(ChronoField.DAY_OF_WEEK, TextStyle.NARROW_STANDALONE)
在我的测试(JDK 1.8.0_144)中它返回(并且还解析)3
:
DateTimeFormatter parser = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss EEEE MMMM ZZ ccccc", Locale.ENGLISH);
ZonedDateTime date = ZonedDateTime.parse(input, parser);