Comparing dates

Asked

Viewed 813 times

0

My problem is that I am not sure how to compare two dates. I would like to compare a date that the user types, compared to the current date.

If the date type is less than the current one, the deadline is past.

If the date type is equal or higher, the deadline is to expire.

How could I do that ?

what I tried was:

 package calendar;

import com.toedter.calendar.JDateChooser;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ValidaData extends JFrame {

    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    //JFormattedTextField ft = new JFormattedTextField();
    private final JDateChooser data = new JDateChooser();
    JButton jbCalc = new JButton("Validar");
    JLabel label = new JLabel();

    public ValidaData() {
        setSize(455, 265);
        montaData();
        acao();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public JComponent montaData() {
        JPanel jpData = new JPanel();
        setContentPane(jpData);
        jpData.setLayout(new BoxLayout(jpData, BoxLayout.Y_AXIS));
        jpData.add(data);
        data.setPreferredSize(new Dimension(100, 20));
        getContentPane().add("North", data);
        jpData.add(data);
        jpData.add(label);
        label.setText("");
        jpData.add(jbCalc);

        return jpData;
    }

    public void valida(String dataStr) {

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date dataDigitada = null;
        try {

            dataDigitada = df.parse(dataStr);
            Date hoje = Calendar.getInstance().getTime();

            if (dataDigitada.compareTo(hoje) > 0) {

                JOptionPane.showMessageDialog(null, "Vencido!");
            } else {
                JOptionPane.showMessageDialog(null, "esta para vencer o prazo!");
            }

        } catch (ParseException e) {
            // Data digitada está no formato invalido
            e.printStackTrace();
        }

    }

    private void acao() {
        jbCalc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {

                valida(data.getDateFormatString());
            }
        });
    }

    public static void main(String[] args) {
        ValidaData data = new ValidaData();
        data.setVisible(true);
    }
}

1 answer

1

public void valida(String dataStr) {

    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date dataDigitada = null;
    try {
        // converte a data no formato texto para o formato data
        dataDigitada = df.parse(dataStr);


        Date hoje = Calendar.getInstance().getTime(); // Obtem a data de hoje

        // Definição do metodo compareTo
        // the value 0 if the argument Date is equal to this Date; 
        // a value less than 0 if this Date is before the Date argument; 
        // and a value greater than 0 if this Date is after the Date argument.
        if (dataDigitada.compareTo(hoje) > 0) {
            // JOptionPane.showMessageDialog(null, "Vencido!");
        } else {
            //JOptionPane.showMessageDialog(null, "esta para vencer o prazo!");
        }

    } catch (ParseException e) {
        // Data digitada está no formato invalido
        e.printStackTrace();
    }

}

Just call the validated method with the date typed by the user.

valida(ft.getText());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.