- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想在客户端验证日期,所以我编写了以下代码。但是,我没有得到异常,而是得到了 2 月 31 日日期字符串的正确日期对象,这显然是一个无效的日期。
public class Test {
public static void main(String[] args) {
String dateFormat = "HH:mm:ss MM/dd/yyyy";
String dateString = "11:30:59 02/31/2015";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat, Locale.US);
try {
LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(date);
} catch (Exception e) {
// Throw invalid date message
}
}
}
输出:2015-02-28T11:30:59
有谁知道为什么 LocalDateTime
解析这个日期而不是抛出异常。
最佳答案
你只需要一个严格的 ResolverStyle
.
Parsing a text string occurs in two phases. Phase 1 is a basic text parse according to the fields added to the builder. Phase 2 resolves the parsed field-value pairs into date and/or time objects. This style is used to control how phase 2, resolving, happens.
示例代码 - 其中 withResolverStyle(ResolverStyle.STRICT)
是重要的变化,同时使用 uuuu
而不是 yyyy
(其中uuuu
是 "year"而 "yyyy"是 "year of era",因此不明确):
import java.time.*;
import java.time.format.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
String dateFormat = "HH:mm:ss MM/dd/uuuu";
String dateString = "11:30:59 02/31/2015";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern(dateFormat, Locale.US)
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(date);
} catch (DateTimeParseException e) {
// Throw invalid date message
System.out.println("Exception was thrown");
}
}
}
关于Java 8 LocalDateTime 正在解析无效日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823368/
我在从两个 LocalDateTime 对象获取秒数时遇到问题。 // a timestammp for the time when the connection was established pu
我有字段 initiationDate,它由 ToStringSerializer 类序列化为 ISO-8601 格式。 @JsonSerialize(using = ToStringSerializ
我有一个初始日期和一个 cron 表达式。我怎样才能找到满足此 cron 表达式的下一个日期?。 String cronExpresion = "* * * * * *" LocalD
我需要做一个 LocalDateTime UTC 日期到另一个日期的转换 LocalDateTime考虑特定时区 tz 的变量。 在我的研究中,我找到了很多解决方案,但它们都转换了 LocalDate
这个问题在这里已经有了答案: Java 8: Difference between two LocalDateTime in multiple units (11 个答案) 关闭 6 年前。 我知道
给定日期时间和时间, LocalDateTime rightDateWrongTime = new LocalDateTime("2017-03-02T15:23:00.000");
将 LocalDateTime 转换为 UTC 中的 LocalDateTime。 LocalDateTime convertToUtc(LocalDateTime date) { //do
这是我的代码: private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss");
我想为一些沉迷于日期值的数据创建一个小型 GUI。所以我在 JavaFX 中使用 LocalDateTimeTextField。所以我将使用以下代码获取选定的时间: LocalDateTimeText
解析 DateTime 时出现异常。我在这里缺少什么 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("0DDDHHmmss");
我有 REST 网络服务公开资源和创建日期。它是用 Java 8 编写的 - 使用 LocalDateTime。 Jackson 2 正在将其序列化为: “创建日期”:[2016, 5, 19, 18
有什么简单的方法可以将 Java 8 的 LocalDateTime 转换为 Joda 的 LocalDateTime? 其中一种方法是将其转换为字符串,然后从该字符串创建 Joda 的 LocalD
我在 Java 8 中使用 LocalDateTime.now() 获取 LocalDateTime。但有时这个 now() 函数会以没有秒的格式返回时间给我。我认为这是因为秒为零,但我需要秒。 代码
Date与LocalDateTime和LocalDate互相转换思路 Date转LocalDateTime和LocalDate都可以通过Date先转换成Instant然后再转换成LocalDateTi
在本教程中,我们将为LocalDate和LocalDateTime类编写自定义序列化程序和反序列化程序。 LocalDateSerializer LocalDateTimeSerializer Loc
从 API 传递的字符串通常遵循格式 yyyy-MM-dd HH:mm:ss.SSS但是当时间戳中有尾随 0 时,它们会被截断,例如 2019-07-16 13:29:15.100转换为 2019-0
我在 Java 8 中使用 spring boot 2.2.6 和 Jackson 2.10.3。我在整个项目中使用 localdatetime 对象。 Jackson 无法正确解析 LocalDat
我有使用年份和一年中的某一天 (1-365/366) 加上一天中的时间的数据,例如 2018-338T14:02:57.47583,而不是年月日。 我正在尝试编写一个输入时间戳的函数,它通过一堆正则表
我正在尝试将以下日期时间字符串解析为 LocalDateTimeObject,但是我无法识别日期时间字符串的格式。 2021 年 10 月 9 日星期六 02:10:23 -0400 Stri
我尝试并失败(或部分成功)为 LocalDateTime 实现扩展功能 这是我的尝试: fun LocalDateTime.isNotBefore(other: ChronoLocalDateTime
我是一名优秀的程序员,十分优秀!