-6

i got this:

SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date data = new Date();

how can be the "data" variable be in the "sdfDate" format?

i need about this output (it must be the current time):

2014-11-10 17:48:20.128
RudiDudi
  • 455
  • 1
  • 7
  • 18

2 Answers2

1

You should do:

sdfDate.format(data);

This is the correct way to use your dateformatter to format a given date.

Ex.

System.out.println("Date: " + sdfDate.format(data));
brso05
  • 13,142
  • 2
  • 21
  • 40
  • i need a Date type not a string, how i can i do? convert it later or is there a better way? – RudiDudi Nov 10 '14 at 17:00
  • 1
    You have a date type already data is the date type. You can format that date in whatever valid way by using SimpleDateFormat. – brso05 Nov 10 '14 at 17:01
  • I'm not sure what you are asking then `Date data = new Date();` this is your Date type. – brso05 Nov 10 '14 at 17:01
  • 2
    @RudiDudi A `Date` object has no concept of what format it's supposed to be in. You leave it as is and format it, receiving a string, when needed. – tnw Nov 10 '14 at 17:07
0

Use the format function on sdfDate. SimpleDateFormat inherits it from DateFormat.

String result = sdfDate.format(data);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875