1

I have the following piece of code:

System.out.println(array[9]);
Date d = df.parse(array[9]);
System.out.println(d.toString());

and the result of this looks like the following:

01.01.2017

Sun Jan 01 00:00:00 CET 2017

My DateFormatter:

DateFormat df = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);

So my question is now why I get the wrong format. First result is a string, which I must convert to date. But I got the wrong format, not the German one (dd.MM.yyyy).

What's wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Captai-N
  • 1,124
  • 3
  • 15
  • 26
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 28 '18 at 12:07

1 Answers1

2

In your example you should use df.format(d) if you plan to convert Date to String. The default Date.toString() method will use the predefined format which you can't control.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thanks. But i must convert from String to date – Captai-N Nov 28 '18 at 11:45
  • 1
    You already converted from `String` to `Date` with `df.parse()`. `Date` is a placeholder. It's unrelated to a display format. If you plan to display the placeholder in specific format you must use `df.format()` method. – Karol Dowbecki Nov 28 '18 at 11:48