Full return of Date type

4

How do I get this return?

2014-08-05 18: 29: 47.757

Using Date .

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

Using this my return is only 2015-02-03 and does not return the hours.

NOTE: I need to get the value like this to pass as timeStamp to the bank.

    
asked by anonymous 03.02.2015 / 15:14

2 answers

7

You must explicitly request the information with Calendar and format it with SimpleDateFormat :

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

See working on ideone .

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

System.currentTimeMillis()

See running on ideone .

Dcoumentation .

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

    
03.02.2015 / 15:20
3

You can use 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);
    
03.02.2015 / 15:24