从数据库加载后,Java Date toString以不同的格式 [英] Java Date toString in different format after loaded from database

查看:272
本文介绍了从数据库加载后,Java Date toString以不同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java持久化之前和之后以可靠的方式比较日期?
我面临的问题是:当我创建一个新的java.util.Date实例时,它的toString()方法返回一个值,包括星期几和时区:

How can I compare dates in a reliable way in Java before and after they are persisted? The problem I face is: When I create a new instance of java.util.Date it's toString() method returns a value including the day of the week and time zone:

Fri Feb 03 10:15:31 CET 2017

当我在数据库表中保留此日期(EDIT:Date对象)并将其加载回实体时,toString方法返回不同的格式,具体取决于数据库类型:

When I persist this date ( the Date object) in a database table and load it back into an entity the toString method returns a different format, depending on the database type:

In case of MySQL date: 2017-02-03
In case of MySQL datetime: 2017-02-03 10:24:34.0

我并不总是能够访问格式化程序,因为toString方法可能会被我的应用程序中的另一个toString方法隐式调用。

I don't always have access to a formatter because to toString method may be called implicitly by another toString method in my application.

这些问题对我而言:


  1. Date对象如何知道哪个使用
    toString时要选择的格式?

  2. 我如何控制新的Date对象,它应该是
    首先代表一个日期(即没有时间分数的一天)?


  3. 数据库中加载对象之前和之后,
    单元测试中持久对象的日期比较的最佳做法是什么?


推荐答案

使用对象,而不是字符串



不要使用字符串将数据传入/传出数据库。使用对象。 JDBC 的目的是将数据库的数据类型转换为数据类型(类)Java。

Use objects, not strings

Do not use strings to pass data to/from your database. Use objects. That is the purpose of JDBC, to convert from the data types of your database to the data types (classes) of Java.

不要使用遗留日期时间类,例如 java.util.Date 因为它们出了名的麻烦,令人困惑和有缺陷。现在遗留下来,取而代之的是 java.time 班级。

Do not use the legacy date-time classes such as java.util.Date as they are notoriously troublesome, confusing, and flawed. Now legacy, supplanted by the java.time classes.


使用toString时,Date对象如何知道要选择哪种格式?

How does the Date object know which format to pick when using toString?

toString 使用的格式是硬编码的,未选中。您总是从该方法获得相同的格式。而且格式选择较差,而现代库和协议使用标准的ISO 8601格式。

The format used by toString is hard-coded, not picked. You always get the same format from that method. And a poor choice of format, whereas modern libraries and protocols use standard ISO 8601 formats.

java.time中许多糟糕的设计选择。日期 toString 方法的行为,该方法将JVM的当前默认时区应用于实际为UTC的值。这会产生一个实际上不存在的时区错觉。

Among the many poor design choices in java.time.Date is behavior of the toString method applying the JVM’s current default time zone to a value that is actually in UTC. This creates the illusion of a time zone that is not actually present.

最好避免这些字符串。在Java和数据库之间传递和获取对象而不是字符串。调用 PreparedStatement :: setObject ResultSet :: getObject 使用 LocalDate Instant 以及其他此类java.time对象。

Better to avoid these strings. Pass and fetch objects rather than strings, between Java and database. Call PreparedStatement::setObject and ResultSet::getObject methods to work with LocalDate, Instant, and other such java.time objects.

LocalDate ld = myResultSet.getObject( … );

如果你的 JDBC驱动程序尚不符合 JDBC 4.2 ,并且不能直接处理java.time对象,简单地使用java.sql类型。通过调用添加到旧类的新方法,立即将这些java.sql对象转换为java.time对象。

If your JDBC driver is not yet compliant with JDBC 4.2, and cannot directly handle java.time objects, fall back to briefly using the java.sql types. Immediately convert those java.sql objects to java.time objects by calling new methods added to the old classes.

java.sql.Date myJavaSqlDate = myResultSet.getDate( … );
java.time.LocalDate ld = myJavaSqlDate.toLocalDate();




如何控制它应代表的新Date对象仅限日期(即没有时间分数的一天)?

How can I control for the new Date object that it shall represent a date only (that is, a day without the time fraction) in the first place?

使用 LocalDate class仅限日期值,没有时间和没有时区。这映射到SQL标准 DATE 类型的等效值。您应该使用仅日期类型来定义数据库中的列。

Use the LocalDate class for a date-only value without time-of-day and without time zone. This maps to equivalent of the SQL standard DATE type. You should be using a date-only type to define the column in your database.


在对象从数据库加载之前和之后的单元测试中比较持久对象的日期的最佳做法是什么?

What is the best practice to compare dates of persisted objects in unit tests between an object before and after loading it from a database ?

LocalDate 类提供比较方法,例如 compareTo isAfter isBefore isEqual 。其他类是相似的。

The LocalDate class offers methods for comparison, such as compareTo, isAfter, isBefore, and isEqual. Other classes are similar.

在Stack Exchange上,所有这些都被许多次覆盖。请搜索类别名称,例如 LocalDate Instant OffsetDateTime ZonedDateTime ZoneId java.sql.Timestamp

This has all been covered many times on Stack Exchange. Please search for class names such as LocalDate, Instant, OffsetDateTime, ZonedDateTime, ZoneId, and java.sql.Timestamp.

这篇关于从数据库加载后,Java Date toString以不同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆