Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java util.Date -> sql.Date

  • 28-07-2009 04: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, Registered Users 2 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