Pessoal Bom dia!
Ainda sou iniciante em java e estou tendo dificuldade para trabalhar com horas. Na minha classe dos get e set eu coloquei o tipo para pegar a hora como String e por esse motivo, no meu JFrame eu só consigo comparar com o banco horas iguais e não o período igual. Por exemplo se eu seleciono um período das 14:00 às 15:00 e esse período já estiver cadastrado no banco ele funciona, mas se eu colocar 14:01 às 15:00 ele passa. Não consigo fazer o teste de maior e menor com string! Alguma luz?
Minha classe com os Get e Set:
public class Destino {
private int id;
private String nome;
private String hora;
private String data;
private String motivo;
private String motorista;
private String area;
private String carro;
private String placa;
private String unidade;
private String email;
private String acomp;
private String horaS;
private String horaR;
private String status;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the nome
*/
public String getNome() {
return nome;
}
/**
* @param nome the nome to set
*/
public void setNome(String nome) {
this.nome = nome;
}
/**
* @return the hora
*/
public String getHora() {
return hora;
}
/**
* @param hora the hora to set
*/
public void setHora(String hora) {
this.hora = hora;
}
/**
* @return the data
*/
public String getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(String data) {
this.data = data;
}
/**
* @return the motivo
*/
public String getMotivo() {
return motivo;
}
/**
* @param motivo the motivo to set
*/
public void setMotivo(String motivo) {
this.motivo = motivo;
}
/**
* @return the motorista
*/
public String getMotorista() {
return motorista;
}
/**
* @param motorista the motorista to set
*/
public void setMotorista(String motorista) {
this.motorista = motorista;
}
/**
* @return the area
*/
public String getArea() {
return area;
}
/**
* @param area the area to set
*/
public void setArea(String area) {
this.area = area;
}
/**
* @return the carro
*/
public String getCarro() {
return carro;
}
/**
* @param carro the carro to set
*/
public void setCarro(String carro) {
this.carro = carro;
}
/**
* @return the placa
*/
public String getPlaca() {
return placa;
}
/**
* @param placa the placa to set
*/
public void setPlaca(String placa) {
this.placa = placa;
}
/**
* @return the unidade
*/
public String getUnidade() {
return unidade;
}
/**
* @param unidade the unidade to set
*/
public void setUnidade(String unidade) {
this.unidade = unidade;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the acomp
*/
public String getAcomp() {
return acomp;
}
/**
* @param acomp the acomp to set
*/
public void setAcomp(String acomp) {
this.acomp = acomp;
}
/**
* @return the horaS
*/
public String getHoraS() {
return horaS;
}
/**
* @param horaS the horaS to set
*/
public void setHoraS(String horaS) {
this.horaS = horaS;
}
/**
* @return the horaR
*/
public String getHoraR() {
return horaR;
}
/**
* @param horaR the horaR to set
*/
public void setHoraR(String horaR) {
this.horaR = horaR;
}
/**
* @return the status
*/
public String getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(String status) {
this.status = status;
}
}
Minha Classe DAO (Apenas o Adicionar):
package br.com.dao;
import br.com.conexao.ConnectionFactory;
import br.com.destino.Destino;
import br.com.telas.TelaPrincipal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author elivelton.s
*/
public class DestinoDao extends TelaPrincipal {
Connection connection;
public DestinoDao (){
this.connection = new ConnectionFactory().getConnection();
}
public void Adicionar (Destino destino){
String sql = "INSERT INTO destino (nome_dest, hora_dest, data_dest, motivo_dest, motorista_dest, area_dest, carro_dest, placa_dest, unidade_dest, acomp_dest, horaS_dest, horaR_dest, statu) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
try {
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, destino.getNome());
pst.setString(2, destino.getHora());
pst.setString(3, destino.getData());
pst.setString(4, destino.getMotivo());
pst.setString(5, destino.getMotorista());
pst.setString(6, destino.getArea());
pst.setString(7, destino.getCarro());
pst.setString(8, destino.getPlaca());
pst.setString(9, destino.getUnidade());
pst.setString(10, destino.getAcomp());
pst.setString(11, destino.getHoraS());
pst.setString(12, destino.getHoraR());
pst.setString(13, destino.getStatus());
pst.execute();
} catch (Exception e) {
System.out.println("Erro ao ISERIR "+e.getMessage());
}
}
Ação do Botão Reservar no Meu JFrama:
private void btnReservarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
btnReservar.setEnabled(false);
btnSair.setEnabled(false);
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
// String dataFormatada = simpleDateFormat.format(txtData1.getDate());
Destino destino = new Destino();
destino.setMotorista((String) ComoMoto.getSelectedItem());
destino.setArea((String) ComboArea.getSelectedItem());
destino.setData(((JTextField) txtData1.getDateEditor().getUiComponent()).getText());
destino.setHora(txtHora.getText());
destino.setNome(txtDestino.getText());
destino.setCarro(txtVeiculo.getText());
destino.setPlaca(txtPlaca.getText());
destino.setUnidade(txtUnidade.getText());
destino.setAcomp(txtAcom.getText());
destino.setMotivo(txtMotivo.getText());
destino.setHoraS(txtHoraChegada.getText());
destino.setHoraR(txtHoraRe.getText());
destino.setStatus("RESERVADO");
String sql1 = "Select horaS_dest, horaR_dest, data_dest, carro_dest FROM destino WHERE horaS_dest='" + txtHoraChegada.getText() + "'"
+ " and horaR_dest='" + txtHoraRe.getText() + "' and data_dest='" + ((JTextField) txtData1.getDateEditor().getUiComponent()).getText() + "' and carro_dest='" + txtVeiculo.getText() + "'";
try {
PreparedStatement pst = connection.prepareCall(sql1);
rs = pst.executeQuery();
while (rs.next()) {
hora1 = (rs.getString("horaR_dest"));
hora2 = (rs.getString("horaS_dest"));
data = (rs.getString("data_dest"));
carro = (rs.getString("carro_dest"));
}
} catch (Exception e) {
System.out.println("Erro ao Selecionar " + e.getMessage());
}
if (txtUnidade.getText().equals("") || (Validar_hora(txtHoraRe.getText()) == false) || (Validar_hora(txtHoraChegada.getText()) == false)
|| (ComboArea.getSelectedItem().equals("<<Selecione>>")) || (txtData1.getDate().equals(""))
|| (txtHoraRe.getText().equals(":")) || (txtHoraChegada.getText().equals(":")) || (txtMotivo.getText().equals("")) || (txtVeiculo.getText().equals(""))
|| (ComoMoto.getSelectedItem().equals("<<Selecione>>")) || (txtDestino.getText().equals("")) || (txtPlaca.getText().equals(""))
|| ComboEmailE.getSelectedItem().equals("<<Selecione>>") || txtAcom.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Campos com * Obrigatórios ou Hora Inválida");
} else { //Tratamento se caso já existe uma reserva no banco de dados para a data escolhida ou com a data do dia
if (txtHoraRe.getText().equals(hora1) && (txtHoraChegada.getText().equals(hora2))
&& (txtData.getText().equals(((JTextField) txtData1.getDateEditor().getUiComponent()).getText())) && (txtVeiculo.getText().equals(carro))) {
JOptionPane.showMessageDialog(null, "Horário Reservado ou Período Reservado para esse Veículo!");
} else if (((JTextField) txtData1.getDateEditor().getUiComponent()).getText().equals(data)) { //Tratamento em caso da data for igual a data que já estiver com reserva
JOptionPane.showMessageDialog(null, "Horário Reservado!");
dispose();
TelaReservas tela = new TelaReservas(null, rootPaneCheckingEnabled);
tela.setVisible(true);
} else {
DestinoDao destinoDao = new DestinoDao();
destinoDao.Adicionar(destino);
JOptionPane.showMessageDialog(null, "Reserva Realizada com Sucesso!");
}
