Format Date Return with Date and Non-string Format

Asked

Viewed 1,536 times

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 "30/04/1986", but no use.

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

I’m creating 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 at Get of the Suppliers Class I tried everything, but I get to that point without 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, all legal, with the SimpleDataFormat gets it right, though, I’d like to return a Date. Someone can give me a strength?

  • 2

    I think this is not possible, since in a Date always returns the full timestamp information. I think only via string same. See this answer on Soen

  • What is the type of activities and how you populate it?

  • This way you only get as String. What you can do is to have two get’s, one that returns Date and the other that returns String. Ex: public Date getInitionActivities() { Return Homeactivities} public String getInitiesFormated() { //Your logic with Simpledateformat}

  • Thanks Hugo! I think this way it will work.

  • But how would I do that Hugo.... Now that I stopped to think, I ended up getting confused...

  • When I do the search, it will return the getInicioAtivities... how do I search get the getInicioAtivitiesFormated?

  • Put the error there, that if it is a code problem, it is easier to solve Can you do yes, in the saved String bank? if yes... then you need to make a convert to Hibernate there meets you. , if it is already in date form, then there is no problem, only when it is to show use your method, if it is saved in the database for String and has the permission to fix, do this change to date

  • The idea they gave was to do the following, create the following method: public String getInicioFormated() throws Parseexception { Simpledateformat sdf = new Simpledateformat("dd/MM/yyyy"); String data = sdf.format(startAtivities); Return data; } but how do I get this Kra? how do I get the feedback? is to insert in the class.java right? sorry for the silly questions... but I’m beginner...rs

Show 3 more comments

2 answers

1


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

Even if you can’t change the mapping to LocalDate, nothing stops you from converting the Date for LocalDate.

See more about using these classes, convert it back to Date or format as String, in this other question.

0

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

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

Take 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 way out:

Wed Apr 30 00:00:00 BRT 1986

30/04/1986

  • 2

    Either way, Date will ALWAYS return a timestamp, not just the date. The doubt was to return Date only with date and this is not possible without formatting and converting to string.

  • long timestamp = 0; //timestamp of the database date Date data = new Date(timestamp); System.out.println(formatRelaton.format(date));

  • 2

    Do you realize that this is the way you try, always convert into string to format? As I said, impossible Date return only date without the timestamp.

  • Diego, the toString() default output format of the Date object is this (Wed Apr 30 00:00:00 BRT 1986), you need to convert to another format.

  • 1

    What I’m saying is the question was Formatar retorno de data com formato Date e não String and all the shapes you’ve made have ended in string, because it’s not possible.

  • 1

    If you already have Date, you create a String, if you have String, create a Date. It makes no sense to create a Date from another Date.

  • So you confirmed that your answer did not answer what you were asked :)

  • You see, you want to display an object Date only the value "30/04/1986", correct?

  • Not me, the creator of the question. And unless he rewrites Date just to edit the tostring (which would be bullshit), there’s no way to display Date that way.

  • The form is using Simpledateformat (or another library or code itself), or by changing the world ISO format to "day/month/year only" and forcing all programming languages to follow this pattern.

  • Exactly, which I think it would be nice for you to explain in the answer, so that she answers what was questioned.

  • All this for a question that probably won’t even be revisited.

  • 4

    Aggregating information is never enough, and the target of SOPT questions is not only those that ask the questions. Remember that this site is a reference in the area of technology for research, pass good information goes far beyond the OP come back here or not, the content you generate, when it is a good content, does not serve only for one person.

  • Sorry to keep you waiting, guys, I’ve had a busy week. I will try to add the getInicioActivityFormated, indicated up there, I will also look at the feature of java 8 that was mentioned. In fact I would like to have a formatted return date, the most I got was 1986-04-30, using gettime. Thank you all for your help, as soon as you get a way that’s legal I’ll let you know and tell you how it turned out.

  • The idea they gave was to do the following, create the following method: public String getInicioFormated() throws Parseexception { Simpledateformat sdf = new Simpledateformat("dd/MM/yyyy"); String data = sdf.format(startAtivities); Return data; } , but how do I get this Kra? how do I get the return? is to insert it in the.java class right? forgive the silly questions... but I am beginner...rs

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.