• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 657
  • Last Modified:

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);
        }
0
zolf
Asked:
zolf
2 Solutions
 
Ajay-SinghCommented:
java.sql.Date contains *only* date, not time. Use java.sql.Timestamp if you want both
0
 
objectsCommented:
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
 
CEHJCommented:
Much of that code is redundant. The same result can be obtained with just

sqlDate1 = new java.sql.Timestamp(System.currentTimeMillis());
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now