How to convert java.util.Date to java.sql.Timestamp inwards Java - JDBC Example

You tin convert java.util.Date to java.sql.Timestamp yesteryear start taking the long millisecond value using the getTime() method of Date shape in addition to thence move yesteryear that value to the constructor of Timestamp object. Yes, it's equally uncomplicated equally that. For amend code reusability in addition to maintenance, you lot tin exercise a DateUtils or MappingUtils shape to conk along these kinds of utility or mapping functions. Now, the questions comes, why exercise you lot bespeak to convert java.util.Date to java.sql.Timestamp? Well, If you lot are storing appointment values to database using JDBC, you lot bespeak to convert a java.util.Date to its equivalent java.sql.Timestamp value. Even though both of them stand upwards for appointment + fourth dimension value in addition to tin hold upwards stored inwards DATETIME SQL type inwards Microsoft SQl Server database or equivalent inwards other databases similar Oracle or MySQL, in that place is no method inwards JDBC API which takes the java.util.Date object. Instead, you lot direct keep got 3 split methods to laid upwards DATE, TIME, in addition to TIMESTAMP inwards the java.sql package.


Anyway, It's tardily to convert a java.util.Date object to java.sql.Timestamp inwards Java, all you lot bespeak to exercise is telephone phone the getTime() method to acquire the long value from java.util.Date object in addition to move yesteryear it to java.sql.Timestamp constructor, equally shown below:

public Timestamp getTimestamp(java.util.Date date){   return appointment == null ? null : new java.sql.Timestamp(date.getTime()); }

Here I am using the ternary operator of Java to start depository fiscal establishment fit if the appointment is null, if aye thence I am returning null, simply if it's non nix thence I am getting the long millisecond value yesteryear calling date.getTime() in addition to constructing a Timestamp object. Remember, similar to Hashtable, Timestamp likewise doesn't utilization camel case, it's Timestamp in addition to non TimeStamp.



Using ternary operator is likewise a dainty play a joke on to foreclose null pointer exception inwards Java code, without losing readability or adding to a greater extent than lines of code.


Worth noting, Timestamp is a subclass of java.util.Date simply you lot cannot move yesteryear a Timestamp illustration where a java.util.Date is expected because Timestamp shape violates Liskov Substitution Principle. According to which subclass doesn't accolade the superclass contract. You tin read Clean Code yesteryear Uncle Bob Martin to acquire to a greater extent than almost Liskov Substitution regulation in addition to other object-oriented blueprint principles.



Java Program to convert Date to Timestamp inwards JDBC

Now, let's encounter the Java programme to demo how you lot tin convert a Date value to Timestamp value for storing into the database using JDBC API. Remember, if you lot tumble out to utilization both java.sql.Date in addition to java.util.Date on same shape thence uses amount advert i.e. amongst the packet to avoid ambiguity e.g. java.sql.Date

import java.sql.Timestamp; import java.util.Date;  /**  * Java Program to convert java.util.Date to java.sql.Timestamp  *  * @author WINDOWS 8  */ public class DateToTimeStamp {      public static void main(String args[]) {          Date today = new Date();          // converting appointment to Timestamp inwards JDBC         Timestamp timestamp = new Timestamp(today.getTime());         Timestamp t2 = getTimestamp(today);          System.out.println("date: " + today);         System.out.println("timestamp: " + timestamp);         System.out.println("timestamp2: " + t2);      }      /**      * Utility method to convert Date to Timestamp inwards Java      * @param appointment      * @return Timestamp      */     public static Timestamp getTimestamp(Date date) {         return appointment == null ? null : new java.sql.Timestamp(date.getTime());     }  }  Output date: Saturday Mar 12 10:53:26 GMT+08:00 2016 timestamp: 2016-03-12 10:53:26.996 timestamp2: 2016-03-12 10:53:26.996


That's all almost how to convert java.util.Date to java.sql.Timestamp inwards Java. You bespeak this conversion spell storing Date values e.g. appointment of birth, maturity appointment etc into database tabular array where column type is DATETIME. Just recollect that similar to Hashtable, Timestamp likewise doesn't utilization uppercase illustration in addition to if you lot tumble out to utilization both Date classes from java.sql in addition to java.util packet inwards the same shape thence utilization amount advert e.g. java.util.Date for util appointment in addition to java.sql.Date for SQL Date.

Subscribe to receive free email updates:

0 Response to "How to convert java.util.Date to java.sql.Timestamp inwards Java - JDBC Example"

Posting Komentar