mardi 16 juin 2015

Insert date and get it in wrong format

I insert the current date in a table. I have a method to get the current datet:

private static java.sql.Date getCurrentDate() {
        java.util.Date today = new java.util.Date();
        return new java.sql.Date(today.getTime());
     }

Or in some cases I get the date from a from in a String format e.g. 2016-10-12 and then I call this method with action.setTodayInfo(session, getCurrentDate()); to insert to a table the date and some Boolean variables

public void setTodayInfo(HttpSession session, Date date) 
            throws SQLException, ClassNotFoundException {

        System.out.println("Initialize today's info...");

        String sq = "INSERT INTO IsSELECTED (date, morning, noon, night) VALUES (?, ?, ?, ?)";

    try {       
        Class.forName(typeDB);
        c = DriverManager.getConnection(path);            
        stm = c.prepareStatement(sq);

        PreparedStatement stm = c.prepareStatement(sq);

        stm.setDate(1, date);
        stm.setBoolean(2, FALSE);
        stm.setBoolean(3, FALSE);
        stm.setBoolean(4, FALSE);

        int rowsAffected = stm.executeUpdate();

    } catch (SQLException e) { 
    System.out.println(e.getMessage());
    } finally {
    if (stm != null) {
        stm.close();
    }
    if (c != null) {
        c.close();
    }
}          
}

when I look in the table the date field has this 1434473268231 and not the current day in format like 2015/06/16..

The table format is:

CREATE TABLE "IsSELECTED"(
       "date" DATETIME PRIMARY KEY  NOT NULL  DEFAULT (CURRENT_DATE) ,
       "morning" BOOL NOT NULL  DEFAULT (0) ,
       "noon" BOOL NOT NULL  DEFAULT (0) ,
       "night" BOOL NOT NULL  DEFAULT (0) 
 )

Aucun commentaire:

Enregistrer un commentaire