在java.util.Date或java.sql.Date之间进行选择 [英] Choosing between java.util.Date or java.sql.Date

查看:160
本文介绍了在java.util.Date或java.sql.Date之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用java.util.Date还是java.sql.Date?

Should I use java.util.Date or java.sql.Date?

我有一个VisualFox数据库,我使用IntelliJ Idea向导检索了实体适当的jdbc类型4驱动程序。

I have a VisualFox database and I have retrieved the entities with the IntelliJ Idea wizard using an appropiate jdbc type 4 driver.

ide(或驱动程序)已将日期字段创建为Timestamp。但是,日期字段不是时间戳,而是日期字段,它们仅存储年,月和日。

The ide (or the driver) has created the date fields as Timestamp. However, the date fields are not timestamps but Date fields, they store year, month and day only.

所以我想知道是否应该切换到java.util.Date或java.sql.Date。乍一看,我认为java.sql.Date应该是合适的,但它有许多声明为弃用的方法。

So I wonder if I should switch to java.util.Date or java.sql.Date. At first glance I thought that java.sql.Date should be the appropiate one, but it has many methods declared as deprecated.

推荐答案

tl; dr



tl;dr


我应该使用java.util.Date还是java.sql.Date?

Should I use java.util.Date or java.sql.Date?

两者都没有。

JDBC 4.2及更高版本。请改用 java.time 类。

Both are obsolete as of JDBC 4.2 and later. Use java.time classes instead.


  • 仅限日期值 对于类似于SQL标准 DATE 的数据库类型,请使用 java.time.LocalDate


    • LocalDate ld = myResultSet.getObject(...,LocalDate.class);

    • myPreparedStatement.setObject(ld,...);

    • date-only value
      For a database type akin to SQL-standard DATE, use java.time.LocalDate.
      • LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
      • myPreparedStatement.setObject( ld , … ) ;

      • Instant instant = myResultSet.getObject(...,Instant.class);

      • myPreparedStatement.setObject(instant,...);

      • Instant instant = myResultSet.getObject( … , Instant.class ) ;
      • myPreparedStatement.setObject( instant , … ) ;

      问题和其他答案似乎过分考虑了这个问题。 java.sql.Date 仅仅是 java.util.Date 时间设置为 00:00:00

      The question and other answers seem to be over-thinking the issue. A java.sql.Date is merely a java.util.Date with its time set to 00:00:00.

      来自 java.sql.Date doc (斜体文本是我的) ...

      From the java.sql.Date doc (italicized text is mine)…


      课程日期

      Class Date

      java.lang.Object

      java.lang.Object

      java.util.Date ←继承自juDate

          java.util.Date        ← Inherits from j.u.Date

      java.sql.Date

              java.sql.Date

      ...

      一个包含毫秒值的瘦包装器,允许JDBC将其识别为SQL DATE值。毫秒值表示自1970年1月1日00:00:00.000 GMT以来经过的毫秒数。 ←时间设置为零,午夜GMT / UTC

      A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.  ← Time-of-day set to Zero, midnight GMT/UTC

      为了符合SQL DATE的定义,毫秒值包含在必须通过在与实例关联的特定时区中将小时,分钟,秒和毫秒设置为零来对java.sql.Date实例进行规范化。

      To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.



      仅限日期与日期时间



      核心问题是:

      Date-Only versus Date-Time

      The core problem is:


      • SQL
        在SQL中, DATE 数据类型只存储日期,没有时间-of天。

      • JAVA
        在与早期版本的Java捆绑在一起的设计糟糕的日期时间库中,他们未能包含一个代表日期的类 - 只有。

      • SQL
        In SQL, the DATE data type stores a date-only, without a time-of-day.
      • JAVA
        In the badly designed date-time library bundled with the early versions of Java, they failed to include a class to represent a date-only.

      Java团队没有创建仅限日期的类,而是使成为一个可怕的黑客。他们参加了他们的日期时间课程(错误的 java.util.Date 类,包含日期时间)并将其扩展为将实例设置为其每日时间午夜 UTC 00:00:00 。那个hack,即juDate的子类,是 java.sql.Date

      Instead of creating a date-only class, the Java team made a terrible hack. They took their date-time class (the misnamed java.util.Date class, containing both date and time) and extended it to have an instance set its time-of-day to midnight UTC, 00:00:00. That hack, that subclass of j.u.Date, is java.sql.Date.

      所有这些黑客攻击,糟糕的设计和错误的行为都令人困惑。

      All this hacking, poor design, and misnaming has made a confusing mess.

      那么何时使用哪个?简单,经过混乱之后。

      So when to use which? Simple, after cutting through the confusion.


      • 在读取或写入数据库的仅限日期列时,请使用 java。 sql.Date 因为它笨拙地试图掩盖它的时间。

      • 在Java的其他任何地方,您需要时间以及日期,请使用 java.util.Date

      • 如果手头有java.sql.Date但需要java.util.Date,只需传递java.sql.Date即可。作为子类, java.sql.Date java.util.Date

      • When reading or writing to a database’s date-only column, use java.sql.Date as it clumsily tries to mask its time-of-day.
      • Everywhere else in Java, where you need a time-of-day along with your date, use java.util.Date.
      • When you have a java.sql.Date in hand but need a java.util.Date, simply pass the java.sql.Date. As a subclass, a java.sql.Date is a java.util.Date.

      在现代Java中,你现在可以选择合适的日期时间库来取代旧的和臭名昭着的java.util.Date,与Java捆绑在一起的Calendar,SimpleTextFormat和java.sql.Date类。主要选择是:

      In modern Java, you now have a choice of decent date-time libraries to supplant the old and notoriously troublesome java.util.Date, Calendar, SimpleTextFormat, and java.sql.Date classes bundled with Java. The main choices are:

      • Joda-Time
      • java.time
        (inspired by Joda-Time, defined by JSR 310, bundled with Java 8, extended by the ThreeTen-Extra project)

      两者都提供 LocalDate 类来表示日期,没有时间和没有时区。

      Both offer a LocalDate class to represent a date only, with no time-of-day and no time zone.

      更新到JDBC 4.2的 JDBC驱动程序或更高版本可用于直接与数据库交换 java.time 对象。然后我们可以完全抛弃丑陋的混乱,即java.util。*和java.sql。*包中的日期时间类。

      A JDBC driver updated to JDBC 4.2 or later can be used to directly exchange java.time objects with the database. Then we can completely abandon the ugly mess that is the date-time classes in the java.util.* and java.sql.* packages.

      本文解释了Java 8中的JDBC已透明更新,以将SQL DATE 值映射到新的java.time。如果您调用 getObject setObject 方法,则为LocalDate类型。

      This article published by Oracle explains that the JDBC in Java 8 has been updated transparently to map a SQL DATE value to the new java.time.LocalDate type if you call getObject and setObject methods.

      用钝的语言, JDBC 4.2更新规范<的底部/ a>确认该文章,新的映射添加到 getObject setObject 方法。

      In obtuse language, the bottom of the JDBC 4.2 update spec confirms that article, with new mappings added to the getObject and setObject methods.

      myPreparedStatement.setObject( … , myLocalDate ) ;
      

      ...和...

      LocalDate myLocalDate = myResultSet.getObject( … , LocalDate.class ) ;
      



      转换



      该规范还说新的方法已被添加到java.sql.Date类中以来回转换为java.time.LocalDate。

      Convert

      The spec also says new methods have been added to the java.sql.Date class to convert back and forth to java.time.LocalDate.

      • public java.time.instant toInstant()
      • public java.time.LocalDate toLocalDate()
      • public static java.sql.Date valueOf(java.time.LocalDate)

      旧的 java.util.Date java.sql.Date java.sql.Timestamp 始终位于 UTC 。前两个(至少)有一个深埋在源代码中的时区,但仅在表面下使用,例如等于方法,并且没有getter /二传手。

      The old java.util.Date, java.sql.Date, and java.sql.Timestamp are always in UTC. The first two (at least) have a time zone buried deep in their source code but is used only under-the-surface such as the equals method, and has no getter/setter.

      更令人困惑的是,他们的 toString 方法应用了JVM的当前默认时区。所以对于天真的程序员来说,似乎就好像他们有时区但他们没有。

      More confusingly, their toString methods apply the JVM’s current default time zone. So to the naïve programmer it seems like they have a time zone but they do not.

      埋藏的时区和 toString 行为是避免这些麻烦的旧遗留类的众多原因中的两个。

      Both the buried time zone and the toString behavior are two of many reasons to avoid these troublesome old legacy classes.

      使用 java.time (Java 8及更高版本)。在java.time缺少的地方,使用 Joda-Time 。 java.time和Joda-Time都有方便的方法来回传需要的旧类。

      Write your business logic using java.time (Java 8 and later). Where java.time lacks, use Joda-Time. Both java.time and Joda-Time have convenient methods for going back and forth with the old classes where need be.

      替换:

      • java.util.Date is replaced by java.time.Instant
      • java.sql.Timestamp is replaced by java.time.Instant
      • java.sql.Date is replaced by java.time.LocalDate.
      • java.sql.Time is replaced by java.time.LocalTime.

      Instant 类代表 UTC中时间轴上的片刻,分辨率为纳秒(最多十九(9)位小数分数)。

      The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

      所有三个 java.time.Local ... 类都缺少时区从UTC偏移

      java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如 java.util.Date 日历 ,& SimpleDateFormat

      The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

      Joda-Time 项目,现已进入维护模式 ,建议迁移到 java.time classes。

      The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

      要了解更多信息,请参阅 Oracle Tutorial 。并搜索Stack Overflow以获取许多示例和解释。规范是 JSR 310

      To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

      您可以直接与数据库交换 java.time 对象。使用符合 JDBC驱动程序 / jeps / 170rel =nofollow noreferrer> JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql。* 类。

      You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

      从哪里获取java.time班?

      Where to obtain the java.time classes?


      • Java SE 8 Java SE 9 Java SE 10 ,以及之后


        • 内置。

        • 带有捆绑实现的标准Java API的一部分。

        • Java 9添加了一些小功能和修复。

        • Java SE 8, Java SE 9, Java SE 10, and later
          • Built-in.
          • Part of the standard Java API with a bundled implementation.
          • Java 9 adds some minor features and fixes.
          • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
          • Later versions of Android bundle implementations of the java.time classes.
          • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

          ThreeTen-Extra 项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在这里找到一些有用的课程,例如 Interval YearWeek YearQuarter 更多

          这篇关于在java.util.Date或java.sql.Date之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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