Full Date Return Type

Asked

Viewed 686 times

4

How do I get that return?

2014-08-05 18:29:47.757

Using the Date.

Date data = new java.sql.Date(new java.util.Date().getTime());

Using that my return is just 2015-02-03 and does not return the hours.

OBS.: need to get the value so to pass as timeStamp to the bank.

2 answers

7


You need to ask for all information explicitly with Calendar and format it with SimpleDateFormat:

new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.S").format(Calendar.getInstance().getTime())

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In the question edit (which was later removed) says you need the timestamp, there is another simpler solution by taking the timestamp:

System.currentTimeMillis()

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Administration.

Note that we are talking about formatting the date and time presentation. Internally a type date stores all information always. If you are going to use a data of this type, it will always have all the accuracy. The fact that you do not see is a problem of presentation only.

  • I had looked at it, but a doubt remains that it generates a String I can convert to Date?

  • It’s possible, of course, but you need to see what you want to do with it, because it might not be necessary. Having a return with the format you want is different from having the date as you want. The internal representation of the type Date It doesn’t matter to you when you’re using it in your program but how to use it the way you need it. In the example code you put you say you have a return, but this code does not return anything visible. The value presentation is different from the value. But I edited it to answer what you asked in the edit. I think that’s what you really want. Or the question is all wrong

  • I think I might have confused you when editing my question. What’s going on is I need to record this pattern in the bank 2014-08-05 18:29:47.757.

  • Thanks for the @bigown link.

  • If you want to write to the database in a specific format, you will do this in a type of text, maybe a varchar, then you will record a string in it. The first example I put up will revolve for you. If you want to record date, you cannot specify format, even in the database the internal representation is data type problem. Format only makes sense in presentation, in storage. Unless you wanted to store the presentation. But if you want the current date in the database, have him do it.

  • Got it, I believe my column be a @datetime solve if I pass the currentTimeMillis, as in the documentation it says it returns the current date in milliseconds.

  • It’s a possibility. It depends on what you want. But if your problem is to take the current date to put in the database, the most correct is to let the database put such information independent of Java. If you don’t know how to do this, open a question to the database you use. Again, I don’t know your specific situation, so I could be wrong.

  • Finally I understood, I did as you said and it worked! debugged and saw what was not explicit in the return, even without going to the sysout it assigns the value correctly to the variable. Thanks @bigown

  • 1

    I don’t think the comment is worth a separate answer, but I would like to point out that Java 8 has introduced new classes like DateTimeFormatter and LocalDateTime. The idea is that people slowly start using these new Apis.

Show 5 more comments

3

You can use the SimpleDateFormat:

String dataFormatada = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(new Date());

To convert back to Date:

Date data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(dataFormatada);
  • I tried to use this way, I am using a property Homehappened in my Backbean of type Date, and Seto occurrence.setInicioOcurrent(dataformatted) when the return happens he says he can’t work with String.

  • @Wellingtonavelino What type of parameter does this method accept?

  • Type Date follows error The method setInicioOcorrencia(Date) in the type Ocorrencia is not applicable for the Arguments (String)

  • @Wellingtonavelino So you have to pass the object Date same. The method you have to take care to format the output the way it needs.

  • A question I must use java.sql.Date or java.util.Date on my Backingbean? :(

  • @Wellingtonavelino Take a look at this response from the English OS: java.util.Date vs java.sql.Date

  • Thank you so much for the help @André Ribeiro,helped me a lot I understood how Date works, now it’s just evoution!

Show 2 more comments

Browser other questions tagged

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