java - 在 Java 中解析 LocalDate 到日期的问题
问题描述
我正在尝试在我的 Java 代码中解析 LocalDate 的日期,但我不断收到以下错误:
{代码:“unknown.unexpected”,详细信息:“无法在索引 0 处解析文本 '02/28/1936'”,元:null}
我的代码如下:
private Date dateOfBirth;
public SearchByDateCommand(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth != null ? Date.valueOf(dateOfBirth) : null;
}
我在这里做错了什么?
解决方案
用这个:
private Date dateOfBirth;
public SearchByDateCommand(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth != null ? Date.from(dateOfBirth.atStartOfDay(ZoneId.systemDefault()).toInstant()) : null;
}
您必须向 LocalDate 添加时间,解释时区内的日期和时间,获取自纪元以来的秒数/毫秒数,最后,创建一个 java.util.Date。
推荐阅读
- oracle - Oracle apex apex_application.g_f0x 不获取表项
- python - 如何修复'Guild'对象在discord.py模块中没有属性'text_channel'
- apache-spark - IllegalArgumentException:未知消息类型:9 读取“delta”文件时
- .net - HttpApplication.Session 和 HttpContext.Current.Session 不同
- graphql - Graphql发送空字符串
- java - 需要 JTable Cell 的 HTML 渲染的首选尺寸
- python-sphinx - Sphinx 中的第一行缩进
- javascript - 试图理解 javascript 闭包的问题
- jquery - 如何使用服务器的 JSON 响应对象(而不是完整对象)中的属性来填充 Kendo 网格?
- php - Slim PHP 链接到另一个页面上的锚点