Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java util.Date -> sql.Date

Options
  • 28-07-2009 5:55pm
    #1
    Closed Accounts Posts: 3,105 ✭✭✭


    i'm using the below to convert a util.Date to a sql.Date, but keep loosing the time any ideas?
    the util.Date seem to hold the date and time, but the time is lost when i convert it to a sql.Date
    String newDateString = sdf.format(calender.getTime());
    		
    		try{
    			DateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
    			java.util.Date parsedUtilDate = formater.parse(newDateString);  
    			java.sql.Date sqltDate= new java.sql.Date(parsedUtilDate.getTime());
    
    			return sqltDate;
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    


Comments

  • Registered Users Posts: 569 ✭✭✭none


    SQL Date doesn't keep the time part by design, just as SQL Time doesn't keep the date. If you want both, stick to SQL Timestamp.
    java.sql.Date sqlDate= new java.sql.Date(System.currentTimeMillis());
    System.out.println("sqlDate:\t" + sqlDate);
    
    java.sql.Time sqlTime= new java.sql.Time(System.currentTimeMillis());
    System.out.println("sqlTime:\t" + sqlTime);
    
    java.sql.Timestamp sqlTimestamp= new java.sql.Timestamp(System.currentTimeMillis());
    System.out.println("sqlTimestamp:\t" + sqlTimestamp);
    


Advertisement