2

I try to parse a Date from a String:

String dateString = "Fr, 1. Jan";
DateFormat format = new SimpleDateFormat("EE, d. MMM");
Date date = null;
try {
    date = format.parse(dateString);
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(format.format(date));

And what I got as output is these:

Do, 1. Jan

Why did that happen and why isn't it the same output as the input?

4
  • I think day name needs at least 3 letters. Commented Jan 27, 2016 at 15:10
  • Maybe it has something to do with your timezone, but then also the 1. Jan should change (especially since the 1. January wasn't a Donnerstag)... Try printing out the date completly and tell us, what you got. Commented Jan 27, 2016 at 15:15
  • FYI: this becomes reproducible if you initialize the SimpleDateFormat with Locale.GERMAN as a second parameter. Commented Jan 27, 2016 at 15:20
  • 1
    It cannot infer the year from the day of the week. A Date must have a year as it is the number of milli-seconds since epoch. Commented Jan 27, 2016 at 15:32

3 Answers 3

3

You forgot the year. When you parse it, you will get in the year 1970 (the Friday will be ignored). When you parse back, you will parse the date 01 Jan 1970, which was a Thursday. This should work:

    String dateString = "Fr, 1. Jan";
    DateFormat format = new SimpleDateFormat("EE, d. MMM");
    Date date = null;
    try {
        date = format.parse(dateString);
    } catch (Exception e) {
        e.printStackTrace();
    }
    date.setYear(new Date().getYear()); //alternativ: date.setYear(2016);

    System.out.println(date);
    System.out.println(format.format(date));
Sign up to request clarification or add additional context in comments.

Comments

2

A Date in Java begins at 01-01-1970. The 1st January in 1970 was a Thursday, therefore it parses the day as Thursday instead of Friday. You would have to add a year in order to guarantee that it's a Friday.

Comments

2

"Do" is the first 2 letters of "Donnerstag", which is German for "Thursday". Judging by your last name "Baum" (German for "tree"), I'm guessing that is not a coincidence. Also, 1970-01-01 was a Thursday, and if you don't specify the year, you get start-of-epoch.

The date format "E" is the day-of-week as a word - the more "E"'s you specify, the more letters of that word get rendered; "EE" would render "Thursday" as "Do" in a German locale, which is suspect is your default locale.

Your code as-is explodes for me, but this similar code produces the same output you get:

String dateString = "Fri, 1. Jan";
DateFormat format = new SimpleDateFormat("EEE, d. MMM");
Date date = format.parse(dateString);
DateFormat format2 = new SimpleDateFormat("EE, d. MMM", Locale.GERMAN);
System.out.println(format2.format(date));

Output:

Do, 1. Jan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.