javascript - 如何在js-joda中进行严格的LocalDate解析?

标签 javascript jsr310 js-joda

我正在尝试解析 LocalDateString 使用 js-joda 中的严格解析(版本 1.1.1)。我不想接受无效日期的输入,例如 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.LENIENTResolverStyle.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 Demo

更新:

但是js-joda DateTimeFormatter API没有提到这个选项,@JodaStephen 提出的模式 uuuu-MM-dd 效果很好。 Working Js Demo

js-joda api :

 |Symbol  |Meaning                     |Presentation      |Examples
 |--------|----------------------------|------------------|---------------
 | G      | era                        | number/text      | 1; 01; AD; Anno Domini
 | y      | year                       | year             | 2004; 04
 | D      | day-of-year                | number           | 189
 | M      | month-of-year              | number/text      | 7; 07; Jul; July; J

Java api :

 Symbol  Meaning                     Presentation      Examples
  ------  -------                     ------------      -------
   G       era                         text              AD; Anno Domini; A
   u       year                        year              2004; 04
   y       year-of-era                 year              2004; 04
   D       day-of-year                 number            189

最佳答案

模式“yyyy-MM-dd”在 STRICT 模式下不完整,因为它不提供时代。请改用“uuuu-MM-dd”模式。

关于javascript - 如何在js-joda中进行严格的LocalDate解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37125709/

相关文章:

json - 在 Grails 3 JSON/rest-api 中格式化 java.time (jsr310) 对象

javascript - 如何在 js-joda 中比较 LocalDate 和 ZonedDateTime?

c# - 如何根据 asp.net 中的分辨率以编程方式创建填充客户端浏览器的按钮列表

javascript - ng-options 过滤器等于来自输入的 ng-model

javascript - 数据列表 : detect if event input is launched by a click on item or a Key pressed

javascript 不适用于 URL 重定向

jackson - Java 8 DateTime 序列化和 Jackson JSR 310

java - 如何使用 Jackson 数据类型 : JSR310 Deser standalone?