我正在尝试LocalDate
从(版本 1.1.1)String
中使用严格分辨率解析 a。js-joda
我不想接受无效日期的输入,例如2016-05-32
. 但我就是不明白。
我的代码是:
formatter = (new JSJoda.DateTimeFormatterBuilder)
.appendPattern("yyyy-MM-dd")
.toFormatter(JSJoda.ResolverStyle.STRICT);
JSJoda.LocalDate.parse("2016-05-10", formatter);
和错误:
Text '2016-05-10' could not be parsed: Unable to obtain LocalDate from
TemporalAccessor: [object Object], type DateTimeBuilder: 2016-05-10,
at index: 0
ResolverStyle.LENIENT
与此模式的预期相同或ResolverStyle.SMART
工作的代码相同。
formatter = (new JSJoda.DateTimeFormatterBuilder)
.appendPattern("yyyy-MM-dd")
.toFormatter(JSJoda.ResolverStyle.LENIENT);
JSJoda.LocalDate.parse("2016-05-32", formatter); // result 2016-06-01
如何在 中使用严格分辨率js-joda
?
更新:
但是js-joda DateTimeFormatter API没有提到这个选项,uuuu-MM-dd
@JodaStephen 提出的模式可以正常工作。工作 Js 演示
|符号 |含义 |介绍 |例子 |--------|----------------|------------ --------|--------------- | 克| 时代 | 数字/文字 | 1个;01; 广告; 公元 | 是 | 年份 | 年份 | 2004;04 | D | 一年中的一天| 号码 | 189 | 中号 | 一年中的一个月 | 数字/文字 | 7; 07; 七月; 七月; Ĵ
符号 含义 表示示例 ------ ------- ------------ -------- G时代文字AD;公元; 一个 u 年 2004 年;04 y 2004 年;04 D 一年中的第 189 天