Format date return with Date format and no String

6

I'm trying to return a date that is recorded right in the bank (1986-04-30 17:02:00), I try to convert this date to appear only "04/30/1986", but it's no use.

The most I get back is Wed Apr 30 00:00:00 BRT 1986.

To create the list with Hibernate.

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;
}

and in the Get from the vendors class I tried everything, but I get to the maximum up to this point without giving error:

public Date getInicioAtividades() throws ParseException { 
 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
 String data = sdf.format(inicioAtividades);
 Date dataI = sdf.parse(data);
 return dataI;
}

If it were to return a string, everything legal, with SimpleDataFormat is right, however, I would like to return a Date. Can someone give me strength?

    
asked by anonymous 08.01.2017 / 20:39

2 answers

0

You can use Hibernate 5 with the Hibernate for Java 8 . So, in your case, just change the type of the inicioAtividades field and the getter return to java.time.LocalDate .

Even if you can not change the mapping to LocalDate , nothing prevents you from converting Date to LocalDate .

See how to use these classes, convert it back to Date or format as String , in this another question .

    
12.01.2017 / 22:51
0

The way to display the date the way you want it is by using SimpleDateFormat (or another library or your own code).

Note that there is a difference between java.sql.Date (used to write dates in the database and java.util.Date .)

See an example:

try {
    SimpleDateFormat formatoDataBanco = new SimpleDateFormat("yyyy-MM-dd");
    Date dataBanco = formatoDataBanco.parse("1986-04-30");
    SimpleDateFormat formatoRetorno = new SimpleDateFormat("dd/MM/yyyy");

    System.out.println(dataBanco);
    System.out.println(formatoRetorno.format(dataBanco));

} catch (Exception e) {
    e.printStackTrace();
}

The output:

Wed Apr 30 00:00:00 BRT 1986

30/04/1986

    
12.01.2017 / 13:57