Convert Calendar to Date to store it in SQLite database

1

I need to convert a CALENDAR variable to DATE (to get date and time), so after that that DATE variable is saved in an SQLite table that has a DATETIME type.

Clarified this we begin by showing the SQLite table called "file", which I plan contains the data of the documents of a directory hosted on an FTP server

Now, the table is filled by reading the FTP directory and taking attributes from the files (using methods from the commons.net.ftp * libraries) here the code

public void cargarArchivos() {
    Connection con=null;
    FTPClient ftp=null;
    try {
        con=Conector.connect();
        ftp=Conector_FTP.cliente();
        FTPFile[] files = ftp.listFiles();
        for (FTPFile file : files) {

            if (file.isFile()) {
               String name = file.getName();
               long tamanio=file.getSize();
               Calendar fecha=file.getTimestamp();               
             PreparedStatement st= con.prepareStatement("insert into archivo (nombre,tamanio,fechamod) VALUES (?,?,?);");
                st.setString(1, name);
                st.setDouble(2, tamanio);
                st.setDate(3, fecha);
                st.execute();
            }

        }
    } catch (Exception e) {
        // TODO: handle exception
    }
}

The problem occurs when I want to pass the Calendar date variable and use it in a setDate to send it to the database as seen here

I already tried to pause it and several options that I found on the Internet and I can not send the variable to the database

    
asked by angelo1793 24.08.2018 в 18:01
source

1 answer

0

As you put in this answer from stackOverFlown (English version), link , what you have to do is convert the class java.util.Date to java.sql.Date, to do that, you have to do something like this:

   java.util.Date fechaActual= new java.util.Date(System.currentTimeMillis());
   java.sql.Date fechaSql= java.sql.Date.valueOf(fechaActual.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());

Once you have a java.sql.Date you can put in your st.toDate.

    
answered by 24.08.2018 в 18:44