In Java util. Date or Java sql. Select between date

I should use Java util. Date or Java sql. Date?

I have a visual fox database, and I have retrieved entities using the IntelliJ idea wizard using the appropriate JDBC type 4 driver

The IDE (or driver) has created the date field as timestamp However, the date field is not a timestamp, but a date field. They only store year, month and day

So I wonder if I should switch to Java util. Date or Java sql. Date. At first glance, I thought Java sql. Date should be appropriate, but many of its methods are declared deprecated

Solution

This question and other answers seem to think too much about it java. sql. Date is just Java util. Date, the time is set to 00:00:00

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

Date and time

The core issues are:

>Sqlin, SQL, date data type only stores one date, no time. > Java in the poorly designed date library bundled with earlier versions of Java, they do not contain a class to represent dates

Instead of creating a date - only class, the Java team made a terrible hacker They took their date time course (wrong java.util.date course, including date and time) and extended it to an instance to set its time to UTC 00:00:00 midnight The hacker, a subclass of j.u.date, is Java sql. Date.

All these hackers, bad design and misleading make chaos chaos

Which use

So when and which? After simplicity, after chaos

>When reading or writing the date only column of the database, use Java sql. Date because it clumsily tries to mask its time. > Elsewhere in Java, you need a time and your date, using Java util. Date. > When you have a Java sql. Date, but you need a Java util. Date, just pass Java sql. Date. As a subclass, Java sql. Date is a Java util. Date.

Even better

In modern Java, you can choose a decent date and time library to replace the old and notorious Java bundled with Java util. Date,. Calendar, simpletextformat and Java sql. Date class The main options are:

> Joda-Time > java. Time (inspired by joda time, defined by JSR 310, bundled with Java 8, extended to threeten extra project)

Both provide a localdate class to represent a date without time and time zone

Eventually, the jdbc driver will be updated to directly use the new Java Time data type Then we can give up the clown completely. This is Java util.* And Java sql.* Date time class in package

Modern JDBC

I hope JDBC will be updated to Java Time localdate provides getter and setter methods

GetObject of setobject |

This article published by Oracle shows that if you call the GetObject and setobject methods, JDBC in Java 8 has indeed been transparently updated to map the SQL date value to the new Java time. Localdate type

In the dull language, the bottom of the JDBC 4.2 update spec confirms the article and adds a new mapping to the GetObject and setobject methods

exchange

The specification also states that new methods have been added to Java sql. The date class is converted back and forth to Java time. LocalDate.

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

time zone

The old date time classes in Java (Java. Util. Date /. Calendar, Java. SQL. Date /. Timestamp, etc., before Java. Time) have no actual time zone Some time zones are buried deep in their source code, but most purposes are ignored The most confusing thing is that their toString method applies the JVM's current default time zone So for naive programmers, it seems to have a time zone, but they don't

Avoid date time classes in Java util.*& java. In SQL*

This time zone problem is to avoid j.u.date/calendar and j.sql as much as possible Date / timestamp is one of many reasons Using Java Time (Java 8 and later) to write business logic In Java Where time is missing, use joda time java. Both time and joda time can be easily copied using the old classes needed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>