How To Convert Java.Util.Date To Java.Sql.Timestamp?

How To Convert Java.Util.Date To Java.Sql.Timestamp? - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul How To Convert Java.Util.Date To Java.Sql.Timestamp?, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel date and time tutorial, Artikel JDBC, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : How To Convert Java.Util.Date To Java.Sql.Timestamp?
link : How To Convert Java.Util.Date To Java.Sql.Timestamp?

Baca juga


How To Convert Java.Util.Date To Java.Sql.Timestamp?

You tin convert a java.util.Date to java.sql.Timestamp value past times using the getTime() method of Date class. This method provide the long millisecond value from Epoch (1st Jan 1970 midnight) which you lot tin move past times to java.sql.Timestamp to practise a novel example of Timestamp object inwards JDBC. Remember, java.sql.TimeStamp class is a wrapper roughly java.util.Date to allow JDBC to stance it every bit SQL TIMESTAMP value. Only way to practise a Timestamp example is past times passing the long fourth dimension value because the 2d constructor of Timestamp class, which accepts private fields e.g. year, month, date, hour, minute, 2d in addition to nano is deprecated. Timestamp degree tin besides concur up-to nano 2d value. 

Even though, it's a wrapper roughly Date degree in addition to contains both engagement in addition to fourth dimension fields it cannot endure used inwards house of Date because it doesn't abide by equals() contract i.e. when you lot compare Date amongst Timestamp it returns truthful but it provide imitation for same object if you lot compare Timestamp to Date.

In other words, ii example of Date in addition to Timestamp amongst same millisecond value volition provide truthful if you lot compare them using equals() method of Date degree i.e. date.equals(timestamp) volition provide true. But, when you lot alter the guild i.e. compare them using Timestamp's equals() method e.g. timestamp.equals(date) the lawsuit volition endure false. This is against the symmetry property of equals contract which says if x.equals(y) is truthful then y.equals(x) should besides endure true.

This divergence comes due to many cistron in addition to i of them is additional nano 2d precision which Timestamp involve to support. You tin run across my before article close why you lot cannot purpose Timestamp inwards house of Date inwards Java to larn to a greater extent than close this interesting topic.

In this tutorial, you lot volition larn how to convert a java.util.Date to java.sql.TimeStamp in addition to vice-versa.




java.util.Date to java.sql.Timestamp in addition to vice-versa

Here is the code to convert a java.util.Date example to java.sql.TimeStamp object inwards JDBC. The code is using the same technique which nosotros possess got used inwards past times to convert java.util.Date to java.sql.Date i.e. extracting the long millisecond value from Date in addition to using it practise example of Timestamp class

Date directly = new Date(); Timestamp ts = new Timestamp(now.getTime());


And hither is code for reverse side i.e. converting a java.sql.Timestamp value to java.util.Date inwards Java:

Timestamp directly = new Timestamp(); Date engagement = new Date(now.getTime());

You tin run across it's quite simple, zilch complicated, simply a duet of business of code in addition to you lot are done. Though, if you lot desire to larn to a greater extent than close engagement in addition to fourth dimension classes inwards JDBC, I propose you lot to read Core Java Volume 2 - Advanced Features past times Cay S. Horstmann. This mass explains all of import JDBC concepts inwards unproblematic language.




Difference betwixt java.util.Date in addition to java.sql.TimeStamp

As I told you lot before, the java.sql.Timestamp is non precisely same every bit java.util.Date object, because its a composite of java.util.Date in addition to a split nanoseconds value. Only integral seconds are stored inwards the java.util.Date component, fractional seconds i.e. nanos are separate. This way Timestamp.equals(date) may provide false if you lot move past times a Date which is equal inwards value. This is the sole argue why Timestamp cannot endure used inwards house of Date inwards JDBC.

This degree should alone endure used piece reading DATETIME values from database e.g. SQL Server. You tin in addition to so convert that column into a Timestamp object inwards DAO layer in addition to in conclusion into a java.util.Object inwards your application layer.

If you lot desire to larn to a greater extent than close Timestamp degree inwards java.sql bundle in addition to how to purpose it piece interacting amongst database from Java application, delight see Java : How to Program past times Deitel in addition to Deitel, i of the rare consummate mass for Java programmers.

 This method provide the long millisecond value from Epoch  How to convert java.util.Date to java.sql.Timestamp?



Important points

1) The java.sql.Timestamp is equivalent of SQL TIMESTAPM type inwards Java. It besides maps to Types.TIMESTAMP

2) The setTimestamp() method is used to shop Timestamp value from Java to Database.

3) The java.sql.TimeStamp method extends java.util.Date, but its composite of Date + nano 2d value

4) It's possible that date.equals(timestamp) == true but timestamp.equals(date) == false because equals() method of Timestamp degree is non symmetric, hence violates equals contract.

5) The hashCode() method of java.sql.Timestamp degree uses the underlying java.util.Date implementation in addition to thence does non include nanos inwards its computation.

6) Similar to Hashtable which doesn't follow camel case, Timestamp besides has pocket-size 's' i.e. doesn't follow camel example so, don't write TimeStamp.


That's all close how to convert a java.util.Date to java.sql.TimeStamp value inwards JDBC. Remember, fifty-fifty though java.sql.Timestamp extends java.util.Date , its non a type inheritance, in-fact its simply an implementation inheritance. Timestamp degree besides violates Liskov Substitution principle hence you lot cannot purpose Timestamp inwards house of Date inwards Java.

Further Learning
JSP, Servlets in addition to JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 in addition to 2
Java Platform: Working amongst Databases Using JDBC



Demikianlah Artikel How To Convert Java.Util.Date To Java.Sql.Timestamp?

Sekianlah artikel How To Convert Java.Util.Date To Java.Sql.Timestamp? kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel How To Convert Java.Util.Date To Java.Sql.Timestamp? dengan alamat link https://bestlearningjava.blogspot.com/2017/06/how-to-convert-javautildate-to.html

Belum ada Komentar untuk "How To Convert Java.Util.Date To Java.Sql.Timestamp?"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel