Sunday, November 24, 2019

How To Convert Java.Util.Date To Java.Sql.Timestamp Inward Coffee - Jdbc Example

You tin terminate convert java.util.Date to java.sql.Timestamp yesteryear get-go taking the long millisecond value using the getTime() method of Date degree in addition to thus 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, yous tin terminate exercise a DateUtils or MappingUtils degree to choke along these kinds of utility or mapping functions. Now, the questions comes, why exercise yous necessitate to convert java.util.Date to java.sql.Timestamp? Well, If yous are storing appointment values to database using JDBC, yous necessitate 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 terminate move stored inward DATETIME SQL type inward Microsoft SQl Server database or equivalent inward other databases similar Oracle or MySQL, at that topographic point is no method inward JDBC API which takes the java.util.Date object. Instead, yous receive got got 3 dissever methods to prepare DATE, TIME, in addition to TIMESTAMP inward the java.sql package.


Anyway, It's tardily to convert a java.util.Date object to java.sql.Timestamp inward Java, all yous necessitate to exercise is telephone outcry upwards 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 get-go banking concern jibe if the appointment is null, if yep thus I am returning null, only if it's non nix thus 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 exercise camel case, it's Timestamp in addition to non TimeStamp.




Using ternary operator is likewise a dainty fob to preclude null pointer exception inward 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 only yous cannot move yesteryear a Timestamp instance where a java.util.Date is expected because Timestamp degree violates Liskov Substitution Principle. According to which subclass doesn't honor the superclass contract. You tin terminate read Clean Code yesteryear Uncle Bob Martin to acquire to a greater extent than well-nigh Liskov Substitution regulation in addition to other object-oriented blueprint principles.



Java Program to convert Date to Timestamp inward JDBC

Now, let's come across the Java plan to demo how yous tin terminate convert a Date value to Timestamp value for storing into the database using JDBC API. Remember, if yous hap to exercise both java.sql.Date in addition to java.util.Date on same degree thus uses amount refer i.e. amongst the bundle 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 inward 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 inward 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 well-nigh how to convert java.util.Date to java.sql.Timestamp inward Java. You necessitate this conversion piece storing Date values e.g. appointment of birth, maturity appointment etc into database tabular array where column type is DATETIME. Just call upwards that similar to Hashtable, Timestamp likewise doesn't exercise majuscule representative in addition to if yous hap to exercise both Date classes from java.sql in addition to java.util bundle inward the same degree thus exercise amount refer e.g. java.util.Date for util appointment in addition to java.sql.Date for SQL Date.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


No comments:

Post a Comment