0
        Calendar calendar = Calendar.getInstance();         
        java.util.Date now = calendar.getTime();             
        java.sql.Time currentTimestamp = new java.sql.Time(now.getTime());
        Date date = new Date(currentTimestamp.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(date);
        java.sql.Date.valueOf(formattedDate);
        System.out.println(formattedDate);

How to get the current formatted System time(yyyy-MM-dd) and insert it to the table? I already tried my best and search for the answer but its no good I get different error messages like when I insert java.sql.Date.valueOf(formattedDate);I always get java.lang.IllegalArgumentException but when I remove java.sql.Date.valueOf(formattedDate) and change this line "+pcList.get(j).getid()+",0,0 into "+pcList.get(j).getid()+",0,2014-07-07 I always get an error saying Data truncation: Incorrect date value: '2000' for column 'date' at row 1

if(ifLoggedIn){
            for(int j=0;j<b;j++){
            try {
                Connection conn = DriverManager.getConnection(url, "root", "");
                Statement stmt = conn.createStatement();   
                ResultSet rs = stmt.executeQuery("SELECT MAX(transactionID) from transaction");
                while(rs.next()){                
                   tID = rs.getInt("MAX(transactionID)");
                }                
                tID= tID+1;
                stmt.executeUpdate("Insert into ccpdb.transaction(membershipNum,productID,time,date,paymentOption,amount,transactionID,quantity) values ("+currentuser.getMembershipNum()+","+pcList.get(j).getid()+",0,0,\"west\","+tf1.getText()+","+tID+","+pcList.get(j).f3.getText()+")"
                        + ""); 
                conn.close();
                } catch (Exception g) {
                    System.err.println("Got an exception! ");
                    System.err.println(g.getMessage());
                }   
            }
        }

Please help me!!

2 Answers 2

1

Just use the database time, rather than the java time. You can get it using the function now() or CURRENT_DATE. I can't quite tell where you want to put this in the insert, but it can go in the list of values.

Sign up to request clarification or add additional context in comments.

1 Comment

OH MY GOD THAT WAS AN EASY FIX!! I didn't know that we can do that :((( thanks you just saved me
0

Try this:

java.util.Date now = new java.util.Date();
java.sql.Date insertDate = new java.sql.Date(now.getTime());

You can insert this date directly.

You are getting this error as you are trying to save String in date column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.