简体   繁体   中英

how to set datetime from java to sql with Date today = new Date();

I set Date today = new Date(); this value to input on html that show me this Sat May 18 22:42:32 ICT 2019

but in sql server i get the value is 2019-05-18 00:00:00 with smalldatetime type. How can I get the value of the day and hour?

code in Java class

              Date today = new Date();
              Datenow dn = new Datenow();
              dn.setToday(today);
              dateFacade.create(dn);

While storing data are you using java.sql.Date because if it is the case then time value will be truncated. You have to use java.sql.Timestamp instead.

Refer Java Docs

tl;dr

You are mixing the wrong data types in both your database and your Java code, trying to represent a moment yet stripping off crucial zone/offset information. But if you insist:

myPreparedStatement.setObject (
    … ,
    OffsetDateTime.now( ZoneOffset.UTC ).toLocalDateTime()  // Or some other `ZoneId` or `ZoneOffset` object.
) ;

Types in MS SQL Server

The smalldatetime type in Microsoft SQL Server is a non-standard legacy type. The documentation recommends using other types nowadays. Note that future dates are limited to June 6, 2079.

This type represents simply a date and a time-of-day, resolving to whole seconds. So this types does not represent a moment. Without the context of a time zone or offset-from-UTC, we cannot determine a moment. A smalldatetime represents potential moments along a range of about 26-27 hours, the range of time zones around the globe. So for example, 2019-01-23 12:00:00 might mean noon in Tokyo, or noon in Kolkata, or noon in Paris, or noon in Montréal — all of these being different moments several hours apart from one another.

Types in Java

You are using terrible date-time classes ( java.sql.Date or java.util.Date ) that are now legacy, supplanted years ago by the modern java.time classes defined in JSR 310.

The smalldatetime equivalent type in Java is LocalDateTime , a date with time-of-day but no time zone or offset-from-UTC.

LocalDateTime ldt = LocalDateTime.of( 2018 , Month.MAY , 18 , 22 , 42 , 22 ) ;

With JDBC 4.2 and later, we can directly exchange some of the java.time types with the database. I do not use Microsoft SQL Server, so I have not tried this. But you should be able to sent a LocalDateTime Java object to a smalldatetime column.

Use a prepared statement with a ? placeholder in your SQL code.

myPreparedStatement.setObject( … , ldt ) ;

Retrieval.

LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;

Current moment

Date today = new Date();

First, be aware of the difference between java.util.Date and java.sql.Date . The first represents a moment in UTC. The second pretends to represent a date-only without time-of-day and without a zone/offset, but actually has both a time-of-day and a zone/offset as it inherits from the other Date in a horribly bad hack of a class design. You should not be using either Date class.

Apparently you are trying to represent the current moment in your smalldatetime column. This makes no sense. As discussed above, you cannot represent a moment in smalldatetime . So you are inappropriately mixing the wrong types.

If you insist on proceeding, you can fake it I suppose.

Let's capture the current moment.

Instant instant = Instant.now() ;  // Capture the current moment in UTC.

JDBC 4.2 does not require support for Instant . So let's convert that to a OffsetDateTime object specifying an offset-from-UTC of zero hours-minutes-seconds for a moment in UTC itself.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

Shortcut: Capture current moment as OffsetDateTime .

OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;

If you were properly using a column of a type akin to the SQL-standard TIMESTAMP WITH TIME ZONE , you would pass that OffsetDateTime .

myPreparedStatement.setObject( … , odt ) ;

But we need to strip off the offset-from-UTC info as a hack to fit your smalldatetime column.

LocalDateTime ldt = odt.toLocalDateTime() ;  // Data loss: Discarding valuable information about offset-from-UTC.

Send that LocalDateTime object to database as mentioned above.

myPreparedStatment.setObject( … , ldt ) ;

Shortcut that I do not recommend: Call now on LocalDateTime . In my opinion, including the method LocalDateTime.now in the java.time API was a mistake. I cannot imagine when calling that is appropriate. Doing so in this case will be confusing to the reader as we would not know if you comprehended the issues involved in trying to store a moment yet failing to include zone/offset info.

But if you insist against my advice:

myPreparedStatement.setObject (
    … ,
    LocalDateTime.now( ZoneOffset.UTC )  // Or some other `ZoneId` or `ZoneOffset` object.
) ;

About java.time

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 .

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

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

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.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM