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?
– Wellington Avelino
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– Maniero
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.
– Wellington Avelino
Thanks for the @bigown link.
– Wellington Avelino
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.– Maniero
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.
– Wellington Avelino
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.
– Maniero
Let’s go continue this discussion in chat.
– Wellington Avelino
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
– Wellington Avelino
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
andLocalDateTime
. The idea is that people slowly start using these new Apis.– Anthony Accioly