0
Good morning, everyone! I am trying to return a formatted date, is in the database as date, but for return could be a string msm. Some friends have indicated two modes, either creating a new method for formatting or using java 1.8 Localdate.
The problem is that, as I am beginner and I end up lost in some things, I do not know how to return getFormatado... I will put the code that I think will explain better... I’m getting back with Hibernate....
package model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.ForeignKey;
@Entity
@Table(name ="fornecedor")
public class Fornecedor extends Pessoa{
@Column
private Integer codigo;
@Column
private String pessoaContato;
@Column
private String cnpj;
@Column
private Date inicioAtividades;
// ********* Omiti os Get and Setters desnecessários pra pergunta :)
public Date getInicioAtividades() {
return inicioAtividades;
}
public void setInicioAtividades(Date inicioAtividades) {
this.inicioAtividades = inicioAtividades;
}
// ********** Pediram pra eu acrescentar este método aqui, abaixo
public String getInicioFormatado() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String data = sdf.format(inicioAtividades);
return data;
}
The problem is, how do I call this Kra, since in Hibernate is like this:
public List<Fornecedor> listarFornecedores() {
session = HibernateUtil.getSessionFactory().openSession();
List<Fornecedor> listaFornecedores = new ArrayList<Fornecedor>();
query = session.createQuery("FROM Fornecedor");
listaFornecedores = query.list();
session.close();
return listaFornecedores;
Can someone give me that light there? Personal thank you!!!
Thanks for the reply, friend, it was very complete... but I still can not understand, it’s how I get the return of getInicioAtivitiesFormated, since there is no note of Hibernate and it is also not in the bank... I use Hibernate 5 :)
– sounobre
I edited the answer to show how to get the return.
– Felipe Marinho
Friend, Thank you. I will try here and after lunch I give an answer. Hug!
– sounobre
Sorry to keep you waiting. What I still can’t understand, is how I will do at the time when the select is run in Hibernate and the list is created, in which moment I call getInicioActivitiesFormated and I do the conversion, to enter the list in place of Homeactivities.
– sounobre
@So you want him to enter the village of
inicioAtividades
? I can’t imagine a scenario where that would be necessary. You could give the example of a situation where you think this should be done?– Felipe Marinho