Store a Java Date value in MySQL

2

I'm trying to store a java.util.Date value that contains a date and time together in a MySQL record. I've tried this:

java.util.Date d = new java.util.Date();  
plantilla = new SimpleDateFormat("dd/MM/yyyy H:mm");
String tiempo = plantilla.format(d);

And then write to MySQL the value tiempo but it does not work. I do not know if I have to use the class java.sql.Date . In any case I'm lost.

Can I directly burn a java.util.Date value in MySQL by defining the field with another type than DATETIME or not recommended?

    
asked by Oundroni 22.05.2016 в 11:39
source

4 answers

1

You can do it, if your field in the database is a date, you have to convert from java.util.Date to java.sql.Date

java.sql.Date date2 = new java.sql.Date(d.getTime());

So you can use a preparedStatement and use date2 for the insert

    
answered by 22.05.2016 / 16:10
source
2
  

Can I directly record a java.util.Date value in MySQL directly by defining the field with a different type than DATETIME or is it not recommended?

JDBC does not support direct instance registration of java.util.Date . Instead, you have three classes in the java.sql package that extend from java.util.Date so you can perform the operation:

  • java.sql.Date . This guy only stores the date. The time information is lost. Equivalent to type sql DATE .
  • java.sql.Time . This guy only stores the time. The date information is lost. Equivalent to the sql 'TIME type.
  • java.sql.Timestamp . This type stores both date and time. Equivalent to type sql DATETIME and TIMESTAMP , depending on the support that the database engine has.

For your case, I recommend you create an instance of Timestamp from your java.util.Date object like this:

java.util.Date miObjetoJavaUtilDate = ...
Timestamp timestamp = new Timestamp(miObjetoJavaUtilDate.getTime());
PreparedStatement pstmt = ...
//...
pstmt.setTimestamp(1, timestamp);

And to get an object java.util.Date from Timestamp :

PreparedStatement pstmt = ...
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
    Date miFechaYHora = new Date(rs.getTimestamp(1).getTime());
}

You can create and get instances of the other classes java.sql.Date and java.sql.Time similarly.

    
answered by 22.05.2016 в 22:22
0

1.- The first step in your database is to observe that the field where the date is going will be in date or datetime format, which is what allows you to save the dates.

2.- You must make a conversion within the program. This is to assign the value:

try {
     String formato = comboFormato.getSelectedItem().toString();
     //Formato
     Date date = SelectorFecha.getDate();
     SimpleDateFormat sdf = new SimpleDateFormat(formato);
     txtFechaSeleccionada.setText(sdf.format(date));
} catch(NullPointerException ex) {
     JOptionPane.showMessageDialog(this, "¡Al menos selecciona una fecha válida!", "¡Error!", JOptionPane.INFORMATION_MESSAGE);
}

3.- Then you must separate the fields to compose the date:

int año = SelectorFecha.getCalendar().get(Calendar.YEAR);
int mes = SelectorFecha.getCalendar().get(Calendar.MONTH) + 1;
int dia = SelectorFecha.getCalendar().get(Calendar.DAY_OF_MONTH);
txtDía.setText("" + dia);
txtMes.setText("" + mes);
txtAño.setText("" + año);

4.- In the end this values the concatenates and the result you give it to your variable type date

Annex: link

    
answered by 09.06.2016 в 21:40
0
public class Cliente {
    private String CODIGO_CLIENTE;  
    private int ESTATUS_CLIENTE ; 
    private String RUC;
    private String NOMBRE ; 
    private String DIRECCION ; 
    private String CIUDAD  ;
    private Date FECHA_INGRESO ;
    private int TIPO_CLIENTE ; 
    private  float LIMITE_CREDITO ; 
    private String CORREO_ELECTRONICO ; 
    private String TELEFONO ;

    public Cliente(){
        CODIGO_CLIENTE="";  
        ESTATUS_CLIENTE =0; 
        RUC="";
        NOMBRE="" ; 
        DIRECCION="" ; 
        CIUDAD ="" ; 

        FECHA_INGRESO=;quiero inicializarlo y no me sale no se is puedas ayudarme

        TIPO_CLIENTE = 0; 
        LIMITE_CREDITO =0; 
        CORREO_ELECTRONICO="" ; 
        TELEFONO ="";
    
answered by 02.11.2017 в 03:14