Sunday, March 29, 2020

Jdbc - How To Convert Java.Sql.Date To Java.Util.Date Inwards Coffee Amongst Example

How to convert java.sql.Date into a java.util.Date in addition to vice-versa is a pop JDBC interview question which is equally good asked equally follow-up interrogation of the difference betwixt java.sql.Date in addition to java.util.Date which nosotros bring saw inwards our final article. Since both SQL appointment in addition to Util appointment stores values equally a long millisecond , it's tardily to convert them dorsum in addition to forth. Both java.sql.Date in addition to java.util.Date provides a convenient method called getTime() which returns long millisecond equivalent of wrapped appointment value. Here is a quick illustration of converting java.util.Date to java.sql.Date in addition to and then dorsum to util Date. This article is inwards side past times side of before post service on Java Date in addition to Time API  like How to convert Date to String in addition to How to honour electrical flow Date in addition to Timestamp inwards Java on whatever timezone. if y'all haven't read them already y'all may honour them useful.



Java Example to convert java.sql.Date to java.util.Date

 Since both SQL appointment in addition to Util appointment stores values equally a long millisecond  JDBC - How to Convert java.sql.Date to java.util.Date inwards Java amongst ExampleAs stated higher upwardly nosotros bring used getTime() method inwards this illustration to transfer long millisecond value shape ane Date type, java.sql.Date to around other e.g. java.util.Date, inwards lodge to convert SQL Date into util Date:





public class DateConverter {

    public static void main(String args[]) throws InterruptedException {
     
        //creating instances of java.util.Date which represents today's appointment in addition to time
        java.util.Date straight off = new java.util.Date();
        System.out.println("Value of java.util.Date : " + now);
     
        //converting java.util.Date to java.sql.Date inwards Java
        java.sql.Date sqlDate = new java.sql.Date(now.getTime());
        System.out.println("Converted value of java.sql.Date : " + sqlDate);
     
        //converting java.sql.Date to java.util.Date back
        java.util.Date utilDate = new java.util.Date(sqlDate.getTime());
        System.out.println("Converted value of java.util.Date : " + utilDate);
    }
 
}

Output:
Value of java.util.Date : Tue April 10 00:12:18 VET 2012
Converted value of java.sql.Date : 2012-04-10
Converted value of java.util.Date : Tue April 10 00:12:18 VET 2012


That's all on how to convert java.sql.Date into a java.util.Date inwards Java. equally y'all bring seen its pretty tardily to convert them past times using getTime() method. There is around other means to convert them using String value of ane Date in addition to and then converting String to java.util.Date merely that's non recommended in addition to using getTime() is to a greater extent than convenient than that.


Further Learning
JSP, Servlets in addition to JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 in addition to 2
Top 10 JDBC Best practices for Java developer

No comments:

Post a Comment