-1

I have a problem with date converting. I use the following program and I expect the output: 19.05.2017

But the output is: 05.00.2017

Can anybody help?

String t = "Fri May 19 00:00:00 CEST 2017";
Date d = new SimpleDateFormat("EEE MMM DD hh:mm:ss zzzz YYYY", Locale.US).parse(t);
String s = new SimpleDateFormat("dd.mm.yyyy").format(d).toString();
System.out.println(s);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Joedi
  • 1
  • 1
  • http://stackoverflow.com/questions/11097256/how-to-convert-mon-jun-18-000000-ist-2012-to-18-06-2012 – PoisonedYouth May 20 '17 at 16:22
  • 2
    The formats are case-sensitive. Use `yyyy` for year, `dd` for day of month and `MM` for month. – Mick Mnemonic May 20 '17 at 16:39
  • 1
    You need to read the javadoc of [`SimpleDateFormat`](http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) more carefully, Take special care for lower-case and upper-case in the patterns. – Thomas Fritsch May 20 '17 at 16:56
  • As an aside, `.toString()` is superflous (and might confuse some) since `format(d)` is already returning a `String`. – Ole V.V. May 20 '17 at 22:03

2 Answers2

1

A surprising result. The oldfashioned classes SimpleDateFormat and friends are full of surprises. This is meant as a negative thing.

Uppercase DD is day of year. Lowercase hh is hour of AM or PM (1 through 12). Uppercase YYYY is weekbased year (only useful with week number). So you are asking for a date that is a Friday in May and the 19th day of the year. Obviously this is not possible.

The result of parsing is Thu Jan 05 23:00:00 CET 2017. Apparently SimpleDateFormat opts for giving you a Friday and for using the zone offset of 2 hours implied by CEST even though the date it has chosen is not at the time of year where CEST (summer time) is in use. I don’t know whether it just gives you the first Friday of the weekbased year (Friday in week 1 of the year). Friday at 0000 hours at offset GMT+2 equals Thursday at 23 at GMT+1, which is CET.

Next for the formatting, 05 is the date as expected, but lowercase mm means minutes. Since the minutes are 0, you get 00. You got the right year.

Rather than using the outdated classes that give you such surprises, I agree with Sam’s answer that you should use the newer classes in java.time:

    ZonedDateTime dt = ZonedDateTime.parse(t,
            DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US));
    String s = dt.format(DateTimeFormatter.ofPattern("dd.MM.uuuu"));

This code gives you 19.05.2017 as you had expected. One of the good things about the modern classes is, if you try to parse with your original format pattern string, you will get a DateTimeParseException so you will know something is wrong. I certainly prefer an exception over incorrect output.

Another good thing is these classes respect the time zone in the input and use it in the output too (unless you explicitly instruct them otherwise). They will never turn Friday 6 January into Thursday 5 January because of some funny time zone issue.

Community
  • 1
  • 1
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Your input date is in Central European Summer Time and your date format is a bit wrong. Try

SimpleDateFormat input = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzzz yyyy");

You might want to set the timezone on the output date format in order to get the date in the correct local time.

Ideally you'd move over to use a java.time style as shown here:

https://www.mkyong.com/java/java-convert-date-and-time-between-timezone/

Sam
  • 670
  • 1
  • 6
  • 20
  • Hi, thank you for your quick answer. I tried with your code, but i get the following error: java.text.ParseException: Unparseable date: "Wed Oct 16 00:00:00 CEST 2013" at java.text.DateFormat.parse(Unknown Source) – Joedi May 20 '17 at 18:55
  • @Joedi, seems Sam forgot to tell you you must still use an English-speaking locale as you do in your question: `new SimpleDateFormat("EEE MMM dd hh:mm:ss zzzz yyyy", Locale.US)`. – Ole V.V. May 20 '17 at 21:59
  • @OleV.V. Thanks for noting that. My omission was caused by a classic case of 'works on my machine'. – Sam May 21 '17 at 01:21