Hello!
Well, I'm using xhtml and primefaces.
In my xhtml page, I have a date field and I use the converter to save to the database, I have a problem with the day.
If I want to save 04/30/1989
In the database saved 1989/04/29
My field is of type Date.
public class Funcionario {
private Long id;
private String nomeUsuario;
private String senha;
private String nome;
private String cpf;
private Date data;
private Cargo funcao = new Cargo();
private Restricao restricao = new Restricao();
//getters e setters omitidos
}
Any date is always recorded one day less.
In my xhtml page I'm using a jsf converter.
<p:outputLabel for="data" value="Nascimento" />
<p:inputMask id="data" mask="99/99/9999" placeholder="Dia/Mês/Ano"
value="#{BeanFuncionario.funcionario.data}"
requiredMessage="Campo data de nascimente é obrigatório."
required="true">
<f:converter converterId="javax.faces.DateTime" />
</p:inputMask>
However, I did some test insertion using a class with jUnit and I noticed that:
@Test
public void salvar(){
CargoDAO cargo = new CargoDAO();
RestricaoDAO restricao = new RestricaoDAO();
//Cargos e retricoes
List<Cargo> lista = cargo.listar();
List<Restricao> lista2 = restricao.listar();
//CRIA UM USUARIO
Funcionario usuario = new Funcionario();
usuario.setNomeUsuario("jhimmyliborio");
usuario.setSenha("s2mm1s");
usuario.setNome("TESTE DE INCLUSAO 1");
usuario.setCpf("88888888888");
// DEFININDO DATA
Date data = new Date("1989/04/30");
usuario.setData(data);
//DEFINE FUNCAO E RESTRICAO
usuario.setFuncao(lista.get(1));
usuario.setRestricao(lista2.get(1));
// SALVAR
FuncionarioDAO udao = new FuncionarioDAO();
udao.salvar(usuario);
}
If I use Date to set the date, everything happens fine.
IfIuseCalendartosetthedate,Ienduphavingprobelmaswiththemonth.itisincremented+1;
04/30/1989->>1989/05/30
@Testpublicvoidsalvar(){CargoDAOcargo=newCargoDAO();RestricaoDAOrestricao=newRestricaoDAO();//CargoseretricoesList<Cargo>lista=cargo.listar();List<Restricao>lista2=restricao.listar();//CRIAUMUSUARIOFuncionariousuario=newFuncionario();usuario.setNomeUsuario("liborioJhimmy");
usuario.setSenha("s2mm1s");
usuario.setNome("TESTE DE INCLUSAO 2");
usuario.setCpf("99999999999");
// DEFININDO DATA
Calendar c = Calendar.getInstance();
c.set(1989, 04, 30);
Date data = c.getTime();
usuario.setData(data);
//DEFINE FUNCAO E RESTRICAO
usuario.setFuncao(lista.get(1));
usuario.setRestricao(lista2.get(1));
// SALVAR
FuncionarioDAO udao = new FuncionarioDAO();
udao.salvar(usuario);
}
ThisismyDAO.
publicvoidsalvar(Funcionariouser){StringBuildersql=newStringBuilder();sql.append(" INSERT INTO funcionario ");
sql.append("(nome_usuario, senha, nome, cpf, nascimento, cargo_id) ");
sql.append("VALUES (lower(?),lower(?),lower(?),lower(?), ?,lower(?)) ");
try (Connection conexao = new Conexao().getConexao()) {
PreparedStatement pstm = conexao.prepareStatement(sql.toString());
pstm.setString(1, user.getNomeUsuario());
pstm.setString(2, user.getSenha());
pstm.setString(3, user.getNome());
pstm.setString(4, user.getCpf());
pstm.setDate(5, new Date(user.getData().getTime()));
pstm.setLong(6, user.getFuncao().getId());
pstm.executeUpdate();
//Mensagens.msgSucesso("Novo usuário salvo", user.getNomeUsuario());
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
Mensagens.msgErro("Erro", e.getMessage().toString());
}
I do not know how the jsf converter behaves, I've already created my prorpia class that converts the user's page entry to the bank. But it's a conversion error. Could not convert a String to Date.
public static void dataBanco(Funcionario funcionario){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String banco = sdf.format(funcionario.getData());
Date data;
try {
data = sdf.parse(banco);
funcionario.setData(data);
} catch (ParseException e) {
e.printStackTrace();
}
}
Can anyone help me?