英文:
ISO 8601 date format string to Date object in Java
问题
我尝试搜索不同格式的 ISO 8601 日期(仅日期部分),但未能找到包含这些详细信息的答案或材料。
我正在尝试接收 ISO 8601 日期格式(仅日期,例如出生日期)的字符串,然后将其转换为日期对象,然后持久化到数据库。 yyyy-MM-dd 是唯一被认为是 ISO 8601 日期格式的日期格式吗?如果是的话,我可以相应地处理,但是否存在多种 ISO 8601 日期格式?如果是的话,您能否指导如何解析给定的字符串并在Java中将其转换为日期对象的最佳方法?
英文:
I tried searching answer for different formats of ISO 8601 date only but not able to identify answer or material which contains these details
I am trying to receive ISO 8601 date format(date only as its date of birth) string and convert it into date object and later persist in dB. Is yyyy-MM-dd only date format that is considered as ISO 8601 date format. If so I can handle this accordingly but is there multiple date ISO 8601 date formats, if so can you please guide what is the best way to parse given string and convert to date object in Java
答案1
得分: 2
是的,yyyy-MM-dd 是 ISO 8601 格式,用于表示没有时间和时区信息的日期。
要将其存储到使用 JDBC 4.2 或更高版本驱动程序,或者使用诸如 Hibernate 5 等现代 JPA 实现的 SQL 数据库中,您无需使用 Date
对象(无论您是否指的是 java.util.Date
还是 java.sql.Date
)。我建议您使用现代的 Java 日期和时间 API,即 java.time
。用于表示没有时间信息的日期的类是 LocalDate
。您可以直接将其存储到数据库中,无需进行任何转换。
LocalDate
默认解析 ISO 8601 格式,即不需要显式的格式化器:
String receivedString = "1962-11-27";
LocalDate dateOfBirth = LocalDate.parse(receivedString);
System.out.println("Date of birth is " + dateOfBirth);
当我们打印 LocalDate
时,它也会返回 ISO 8601 格式:
Date of birth is 1962-11-27
不过,我还没有告诉您完整的故事。yyyy-MM-dd 是迄今为止最常用的 扩展 格式。还有一种更为紧凑的 基本格式,它省略了连字符:yyyyMMdd。我建议您坚持使用扩展格式获取日期字符串。如果无法这样做,您将需要为解析指定一个格式化器:
String receivedString = "19621127";
LocalDate dateOfBirth = LocalDate.parse(
receivedString, DateTimeFormatter.BASIC_ISO_DATE);
这将给您一个与之前相等且无法区分的 LocalDate
对象。
要使用 JDBC 将数据保存到数据库中:
PreparedStatement pStmt = yourDatabaseConnection.prepareStatement(
"insert into your_table (date_of_birth) values (?);");
pStmt.setObject(1, dateOfBirth);
int insertedRows = pStmt.executeUpdate();
请注意使用的是 setObject()
,而不是 setDate()
。
链接
- Wikipedia 文章:ISO 8601
- Oracle 教程:日期时间 解释了如何使用 java.time。
- Java 规范请求(JSR)310,其中首次描述了
java.time
。 - ThreeTen Backport 项目,将
java.time
回退到 Java 6 和 7(JSR-310 的 ThreeTen)。 - ThreeTenABP,ThreeTen Backport 的 Android 版本
- 问题:如何在 Android 项目中使用 ThreeTenABP,附有非常详细的解释。
- 相关问题:将 java.time.LocalDate 对象插入和从诸如 H2 的 SQL 数据库中提取
英文:
Yes, yyyy-MM-dd is the ISO 8601 format for a date without time of day and without time zone.
To store into an SQL database using either a JDBC 4.2 or later driver or a modern JPA implementation such as Hibernate 5, you have got nothing to use a Date
object for (no matter if you meant java.util.Date
or java.sql.Date
). I recommend that instead you use java.time, the modern Java date and time API. The class to use for a date without time of day is LocalDate
. And you can store one into your database without any conversion.
LocalDate
parses ISO 8601 format as its default, that is, without any explicit formatter:
String receivedString = "1962-11-27";
LocalDate dateOfBirth = LocalDate.parse(receivedString);
System.out.println("Date of birth is " + dateOfBirth);
LocalDate
also gives ISO 8601 format back when we print it:
> Date of birth is 1962-11-27
I haven’t given you the full story yet, though. yyyy-MM-dd is the extended format that is by far most often used. There is also a basic format that is compacter in that it leaves out the hyphens: yyyyMMdd. I suggest that you insist on getting your date string in the extended format. If you cannot do that, you will have to specify a formatter for parsing:
String receivedString = "19621127";
LocalDate dateOfBirth = LocalDate.parse(
receivedString, DateTimeFormatter.BASIC_ISO_DATE);
This will give you a LocalDate
equal to and indistinguishable from the one we had before.
To save into your database using JDBC:
PreparedStatement pStmt
= yourDatabaseConnection.prepareStatement(
"insert into your_table (date_of_birth) values (?);");
pStmt.setObject(1, dateOfBirth);
int insertedRows = pStmt.executeUpdate();
Note the use of setObject()
, not setDate()
.
Links
- Wikipedia article: ISO 8601
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Related question: Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论