Interaction between date time in Java and datetime in SQL Server 2005

Source: Internet
Author: User
Tags truncated

Preface

Environment:
Platform:windows XP
Language:java 1.5
Ide:myeclipse 6.0.1
Database:sql Server 2005 Enterprise en

Introduction

This article focuses on how date time in Java interacts with datetime in SQL Server 2005. The date type involved has
Java.util.Date
Java.sql.Date
Java.sql.Time
Java.sql.Timestamp

Section 1-java.util.date

After some testing, we probably understand the actual usage and limitations of these classes.

Java.util.Date Create instance is not able to directly allow JDBC to be stored in the DB, it will appear as follows exception:
Conversion failed when converting datetime from character string.

To save Java.util.Date to the DB must go through type conversion to become date,time or timestamp under SQL.

Section 2-java.sql.date

Java.sql.Date can only save date to Db,time will be intercepted and replaced by the smallest time value in db (00:00:00).

Code:
Java.util.Date currentdatetime = new Java.util.Date ();
System.out.println (Currentdatetime);
System.out.println (Currentdatetime.gettime ());
Date currentdate = new Date (Currentdatetime.gettime ());
System.out.println (currentdate);
System.out.println (Currentdate.gettime ());
String strSQL = "INSERT into testdatetime values ('" +currentdate+ "')";
Executehelper (strSQL);

Console Output:
Tue 15:51:48 CST 2008
1211269908671
2008-05-20
1211269908671

Datebase Output:
2008-5-20 0:00:00

From result it can be seen that java.sql.Date actually does not intercept time in its instance, because the resulting long value is still the same as the long value of java.util.Date, only the output of time is truncated when the output is displayed or the operation of the DB. 0:00:00 is supplemented by the minimum time of DB for its system.

Section 3-java.sql.time

Java.sql.Time can only say time saved to Db,date will be intercepted and replaced by the smallest date value in db (1900-1-1).

Code:
Java.util.Date currentdatetime = new Java.util.Date ();
System.out.println (Currentdatetime);
System.out.println (Currentdatetime.gettime ());
Time CurrentTime = new Time (Currentdatetime.gettime ());
System.out.println (currenttime);
System.out.println (Currenttime.gettime ());
String strSQL = "INSERT into testdatetime values ('" +currenttime+ "')";
Executehelper (strSQL);

Console Output:
Tue 16:03:52 CST 2008
1211270632312
16:03:52
1211270632312

Datebase Output:
1900-1-1 16:03:52

From result it can be seen that java.sql.Time is not actually intercepting date in its instance, because the resulting long value is still the same as the long value of java.util.Date, only the output of date is truncated when the output is displayed or the operation of the DB. 1900-1-1 is supplemented by the minimum date of the DB's system.

Section 4-java.sql.timestamp

Java.sql.Timestamp can save both date and time to db.

Code:
Java.util.Date currentdatetime = new Java.util.Date ();
System.out.println (Currentdatetime);
System.out.println (Currentdatetime.gettime ());
Timestamp Currenttimestamp = new Timestamp (Currentdatetime.gettime ());
System.out.println (Currenttimestamp);
System.out.println (Currenttimestamp.gettime ());
String strSQL = "INSERT into testdatetime values ('" +currenttimestamp+ "')";
Executehelper (strSQL);

Console Output:
Tue 16:24:40 CST 2008
1211271880796
2008-05-20 16:24:40.796
1211271880796

Datebase Output:
2008-5-20 16:24:40

From the result can be seen Java.sql.Timestamp storage format and java.util.Date different, timestamp format is specifically for the DB operation is defined by the canonical format, that is, DB can only receive the value of this format passed, this can also probably know why JAVA.U Til. Why cannot the date be passed directly to the DB.

Interaction between date time in Java and datetime in SQL Server 2005

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.