2
I have a project to do and I’m not able to convert String
for Date
. I want to compare the current date with the date of the query that is in textField Date, and if the current date has passed the date marked, that means that the query has already been performed and so delete it from the Database. However I am not able to compare dates. Here is the code commented doubts
public void PegaData(String dataa) throws ParseException
{
Date data = new Date(System.currentTimeMillis());
SimpleDateFormat formatardata = new SimpleDateFormat("dd/MM/yyyy");
Date dataA =(Date)formatardata.parse(dataa); //Tentei fazer casting mas nao rolou
if(dataA.after(data)){
btConsulta.setEnabled(true);
System.out.println("data passada");
}
else{
btConsulta.setEnabled(false);
System.out.println("data no prazo");
}
}
returned the exception
(nov 29, 2016 2:56:11 pm projectValueChanged.Agenda listValueChanged GRAVE: null java.text.Parseexception: Unparseable date: " - - ")
Complete Code
public class Agenda extends javax.swing.JFrame {
ManipulaBanco dado;
DefaultListModel l;
String nomeMedico;
public Agenda() {
initComponents();
this.setLocationRelativeTo(null);
this.setResizable(false);
dado = new ManipulaBanco();
Ativacao(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
txtPaciente = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
btAgendar = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtID = new javax.swing.JTextField();
jButton4 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
lista = new javax.swing.JList<>();
txtData = new javax.swing.JFormattedTextField();
txtNascimento = new javax.swing.JFormattedTextField();
txtHoras = new javax.swing.JFormattedTextField();
jLabel5 = new javax.swing.JLabel();
txtConfirma = new javax.swing.JTextField();
btConsulta = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Paciente:");
jLabel2.setText("Data:");
jLabel3.setText("Horário");
btAgendar.setText("Agendar");
btAgendar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAgendarActionPerformed(evt);
}
});
jButton3.setText("Voltar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jLabel6.setText("Data de Nascimento");
jLabel4.setText("N° de Identificação:");
txtID.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtIDActionPerformed(evt);
}
});
jButton4.setText("Ok");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
lista.addListSelectionListener(new
javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent
evt) {
listaValueChanged(evt);
}
});
jScrollPane1.setViewportView(lista);
try {
txtData.setFormatterFactory(new
javax.swing.text.DefaultFormatterFactory(new
javax.swing.text.MaskFormatter("##/##/####")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
try {
txtNascimento.setFormatterFactory(new
javax.swing.text.DefaultFormatterFactory(new
javax.swing.text.MaskFormatter("##/##/####")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
try {
txtHoras.setFormatterFactory(new
javax.swing.text.DefaultFormatterFactory(new
javax.swing.text.MaskFormatter("##:##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
jLabel5.setText("Confirmação");
txtConfirma.setEditable(false);
btConsulta.setText("Consulta ja realizada");
btConsulta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btConsultaActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtPaciente,
javax.swing.GroupLayout.PREFERRED_SIZE, 216,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
12, Short.MAX_VALUE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 105,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(btAgendar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3)
.addGap(79, 79, 79))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(txtID,
javax.swing.GroupLayout.PREFERRED_SIZE, 164,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.
ComponentPlacement.UNRELATED)
.addComponent(jButton4,
javax.swing.GroupLayout.PREFERRED_SIZE, 57,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btConsulta)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2)))
.addComponent(jLabel5,
javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtNascimento,
javax.swing.GroupLayout.PREFERRED_SIZE, 80,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtData,
javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtHoras, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(txtConfirma, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(txtNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtConfirma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18,
Short.MAX_VALUE)
.addComponent(btConsulta)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE
)
.addComponent(btAgendar)
.addComponent(jButton3))
.addGap(20, 20, 20))
);
pack();
}// </editor-fold>
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Principal principal = new Principal();
principal.setVisible(true);
dispose();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
nomeMedico = dado.Medicos(txtID.getText());
if (txtID.getText().equals("") || txtData.getText().equals("")) {
JOptionPane.showMessageDialog(this, "Por favor Insira o seu número
de identificação de médico e a Data");
} else if (nomeMedico == null) {
JOptionPane.showMessageDialog(this, "ID Invalido");
} else {
JOptionPane.showMessageDialog(this,"Seja Bem vindo Dr."+nomeMedico);
carregaLista();
Ativacao(true);
}
}
private void listaValueChanged(javax.swing.event.ListSelectionEvent evt) {
String[] resultado = dado.consultaPaciente(lista.getSelectedValue());
txtPaciente.setText(resultado[0]);
txtNascimento.setText(resultado[1]);
txtHoras.setText(resultado[2]);
txtConfirma.setText(resultado[3]);
boolean confirmacao;
confirmacao=txtConfirma.getText().equals("Confirmado");
if(confirmacao)
{
btAgendar.setEnabled(false);
txtData.setEnabled(false);
try {
PegaData(txtData.getText().replace("/", "-"));
} catch (ParseException ex) {
Logger.getLogger(Agenda.class.getName()).log(Level.SEVERE, null,
ex);
}
}
else{
btAgendar.setEnabled(true);
txtData.setEnabled(true);
}
}
private void txtIDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btAgendarActionPerformed(java.awt.event.ActionEvent evt) {
dado.agendaDia(txtData.getText(), txtPaciente.getText());
JOptionPane.showMessageDialog(this, "Data agendada para o paciente: " +
txtPaciente.getText());
limpaCampo();
}
private void btConsultaActionPerformed(java.awt.event.ActionEvent evt) {
if(txtPaciente.getText().equals("")){
btConsulta.setEnabled(false);
}
else{
btConsulta.setEnabled(true);
dado.deletaPaciente(txtPaciente.getText());
JOptionPane.showMessageDialog(this,"Consulta finalizada com sucesso");
limpaCampo();
}
}
public void limpaCampo() {
txtPaciente.setText("");
txtNascimento.setText("");
txtData.setText("");
txtHoras.setText("");
}
public void carregaLista() {
nomeMedico = dado.Medicos(txtID.getText());
l = new DefaultListModel();
ArrayList<String> usuarios = dado.listarPacientes(nomeMedico);
for (int i = 0; i < usuarios.size(); i++) {
l.addElement(usuarios.get(i));
}
if(l.isEmpty()){JOptionPane.showMessageDialog(this,"Nenhum paciente no
momento");}
else
lista.setModel(l);
}
public void Ativacao(boolean resposta) {
txtPaciente.setEnabled(false);
txtData.setEnabled(resposta);
txtNascimento.setEnabled(false);
txtHoras.setEnabled(false);
}
public void PegaData(String dataa) throws ParseException
{
Date data = new Date(System.currentTimeMillis());
SimpleDateFormat formatardata = new SimpleDateFormat("dd/MM/yyyy");
Date dataA =(Date)formatardata.parse(dataa);
if(dataA.after(data)){
btConsulta.setEnabled(true);
System.out.println("data passada");
}
else{
btConsulta.setEnabled(false);
System.out.println("data no prazo");
}
}
I did this, but returned the same error, the only difference is that instead of "-" returned "/"
– Anderson Henrique