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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java util.Date -> sql.Date

  • 28-07-2009 4: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