Hours at JDateChooser

4

I have 2 JDateChooser, when I do JDateChooser1.getDate returns the selected date with time 00:00:00

When I do JDateChooser2.getDate, it returns the selected date with the time when the selection was made.

I need both JDateChooser to return the hour 00:00:00 .

Annex code, never enters else of if , because the dates are never equal for the difference of hours when making the choice of date.

public void llenaCalendarioTarifas(){
    String consulta = "Insert into calendario tarifas (fecha, id_tarifa, id_tipoCuarto) values ";
    String fecha = new SimpleDateFormat("yyyy-MM-dd").format(dateInicio.getDate());
    String fechaF = new SimpleDateFormat("yyyy-MM-dd").format(dateFinal.getDate());
    String idTarifa ="5";
    Date fInicio = dateInicio.getDate();
    Date fFinal = dateFinal.getDate();
    Calendar cal= Calendar.getInstance();

    while(fInicio.compareTo(fFinal)<= 0){

        if(fInicio.compareTo(fFinal)== -1){
            consulta = consulta + "\n('" + fecha + "', " + idTarifa + ", " + idTipo + "), ";
           // JOptionPane.showMessageDialog(null, consulta);
        }else {
            consulta = consulta + "\n('" + fecha + "', " + idTarifa + ", " + idTipo + ")";
           // JOptionPane.showMessageDialog(null, consulta);
        }
        cal.setTime(fInicio);
        cal.add(Calendar.DATE, 1);
        fInicio = cal.getTime();
        fecha = new SimpleDateFormat("yyyy-MM-dd").format(fInicio);
    }
    JOptionPane.showMessageDialog(null, consulta);
}
    
asked by George Maldonado 29.03.2018 в 03:19
source

1 answer

2

You could try this ...

Date:

String formato = "dd/MM/yyyy";
Date date = JFecha.getDate();
SimpleDateFormat sdf = new SimpleDateFormat(formato);
lblfecha.setText(sdf.format(date));

Time:

String dia = Integer.toString(jdt.getCalendar().get(Calendar.DAY_OF_MONTH));
String mes = Integer.toString(jdt.getCalendar().get(Calendar.MONTH) + 1);
String year = Integer.toString(jdt.getCalendar().get(Calendar.YEAR));
String fecha = (year + "-" + mes+ "-" + dia);
txtfecha.setText(fecha);

The example that you have put in your question I found it very long and complicated to do something so simple. I have done a small program with this exercise and it works perfectly. I still do not understand where you get the system time, in my case it does not happen.

Here is a functional example to get only the dates and know the difference in days between the two:

    // Indicamos que solo puede ingresar numeros, año/mes/día
    // y en el campo de texto se mostrará este patron "____/__/__"
    // mientras no ingrese o seleccione una fecha
    JDateChooser dateChooser1 = new JDateChooser("yyyy-MM-dd", "####-##-##", '_');
    dateChooser1.setBounds(12, 60, 195, 22);
    contentPane.add(dateChooser1);

    JDateChooser dateChooser2 = new JDateChooser("yyyy-MM-dd", "####-##-##", '_');
    dateChooser2.setBounds(225, 60, 195, 22);
    contentPane.add(dateChooser2);

    JButton btnComparar = new JButton("Comparar");
    btnComparar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            // Formato
            SimpleDateFormat dcn = new SimpleDateFormat("yyyy-MM-dd");
            // Obtener fechas seleccionadas tipo String
            String date1 = dcn.format(dateChooser1.getDate());
            String date2 = dcn.format(dateChooser2.getDate());
            // Imprimir
            System.out.println("Fecha 1: " + date1);
            System.out.println("Fecha 2: " + date2);

            // Obtener la diferencia en milisegundos
            long diffMillis = dateChooser1.getDate().getTime() - dateChooser2.getDate().getTime();
            // Transformar milisegundos a días
            long diffDays = TimeUnit.DAYS.convert(diffMillis, TimeUnit.MILLISECONDS);
            // Imprimir
            System.out.println("Diferencia: " + diffDays + " días");
        }
    });
    btnComparar.setBounds(12, 109, 408, 43);
    contentPane.add(btnComparar);

Then, as you use if-else to do something, you could use the variable diffDays to make the comparison. If it is greater than X days, do this, if not, do the other ...

Output:

  

Date 1: 2018-03-31

     

Date 2: 2018-03-01

     

Difference: 29 days

    
answered by 29.03.2018 в 04:10