java date time

hello there,

i am trying to insert the data and time into a table.but only the date is being put not the time.the db i am using is sql server 2005.and the column has type datetime.my java code looks like this.

java.sql.Date sqlDate1 = null;
          SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yy HH:mm:ss" ) ;
        java.util.Date d = new java.util.Date() ;
        try
        {
          currentDate = df.format( d ).toString();
          java.util.Date oDate1 = df.parse(currentDate);
                  long tf1 = oDate1.getTime();
                  sqlDate1 = new java.sql.Date(tf1);
        }
        catch( Exception e )
        {
          System.out.println( e);
        }
zolfAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
use a Timestamp, sql.Date does not store time


java.sql.Timestamp sqlDate1 = null;
          SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yy HH:mm:ss" ) ;
        java.util.Date d = new java.util.Date() ;
        try
        {
          currentDate = df.format( d ).toString();
          java.util.Date oDate1 = df.parse(currentDate);
                  long tf1 = oDate1.getTime();
                  sqlDate1 = new java.sql.Timestamp(tf1);
        }
        catch( Exception e )
        {
          System.out.println( e);
        }
0
 
Ajay-SinghConnect With a Mentor Commented:
java.sql.Date contains *only* date, not time. Use java.sql.Timestamp if you want both
0
 
CEHJCommented:
Much of that code is redundant. The same result can be obtained with just

sqlDate1 = new java.sql.Timestamp(System.currentTimeMillis());
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.