0

I read more questions on the web but I dont' find a solution yet.

I have a String like "14/05/1994", exactly in this format.

I need to covert it into java.util.Date in the same format.

I tried:

    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date dataFrom = new Date();
    dataFrom = df.format("14/05/1994");

But the result is: Sat May 14 00:00:00 CET 1994

It's possibile have as a result: 14/05/1994 not as a String, but as java.util.Date?

Dave
  • 1,428
  • 4
  • 31
  • 49
  • `DateFormat df = new SimpleDateFormat("dd/MM/yyyy");` `Date date = df.parse("14/05/1994");` `String d = df.format(date);` – Tiny Jan 12 '16 at 20:26
  • 1
    @DavideFruci Please search StackOverflow before posting. This topic has been addressed in many hundreds, if not thousands, of Questions and Answers. In brief: Do not conflate a String object with a date-time object. Date-time objects *parse* strings and *generate* strings in various formats. Date-time objects themselves do not have a "format". – Basil Bourque Jan 12 '16 at 23:41

1 Answers1

9

A java.util.Date doesn't have a format. It's just a date.

When you print it out, e.g. using toString(), it uses a default format, which is what you're seeing. But you have that date.

Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");

I don't think that can be your code because DateFormat.format accepts a Date and returns a String, not the other way around. You might mean df.parse, which would get you the results you describe. But if you take your SimpleDateFormat and use its format method on the Date, then you should get back out 14/05/1994 as you want. A java.util.Date doesn't have a format, though.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413