10

I am getting the strangest error, when trying to parse a string as a calendar. It seems that it messes up the Date object which I use to set the result calendar's time. The error is pretty inconsistent (or I see no logic in it). Can anyone point out what I might be doing wrong ?

public class caltest{
public static final SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");

public static void main(String[] args) {
    String date1 = "1992-03-11 12:00:12.123";
    String date2 = "1993-03-11 12:00:12.123";
    String date3 = "1994-03-11 12:00:12.123";
    String date4 = "1995-03-11 12:00:12.123";
    parseStringAsCalendar(date1);
    parseStringAsCalendar(date2);
    parseStringAsCalendar(date3);
    parseStringAsCalendar(date4);
}
public static String calendarToString(Calendar cal) {
    return sdf.format(cal.getTime());
}

public static Calendar parseStringAsCalendar(String s) {
    Date time = null;
    try {
        time = sdf.parse(s);
    } catch (ParseException e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
    System.out.println(time.toString());
    GregorianCalendar ret = new GregorianCalendar();
    ret.setTime(time);

    return ret;
}

}

The output is :

Sun Dec 29 12:00:12 CET 1991
Sun Dec 27 12:00:12 CET 1992
Sun Dec 26 12:00:12 CET 1993
Sun Jan 01 12:00:12 CET 1995
Tegi
  • 708
  • 7
  • 14

2 Answers2

18

You're using YYYY in your format specifier, which is week year (as of Java 7, I believe). You want yyyy, which is just "year". (See the SimpleDateFormat documentation.)

I suspect the rest of the date was out because you tried to also specify the month and day, which aren't really "features" in the week year... if you'd specified the "week of week year" and day of week, it might have given some more sensible results, but only if you really meant to use week years, which I doubt :)

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Both answers were very useful, this one shines more of a light on the underlying problem. I was checking the wrong documentation. – Tegi Mar 22 '12 at 23:44
  • 3
    you just saved me from several hours of fruitless search on the internet! thank you – KoW Nov 13 '13 at 13:47
  • What exactly is the difference between *year* and *week year*? The documentation is a little sparse on this distinction (as is the documentation for Calendar). – tytk Jul 23 '15 at 19:04
  • @tytk: Year is the conventional year, where a year is broken into year/month/day. Week-year is a more "week-of-year" focused way of breaking things down, where each date is represented as "week-year, week of week-year, day-of-week". – Jon Skeet Jul 23 '15 at 21:23
3

Use this:

public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

It's lower case y for year, not upper case Y. With that, the result is:

Wed Mar 11 12:00:12 EST 1992
Thu Mar 11 12:00:12 EST 1993
Fri Mar 11 12:00:12 EST 1994
Sat Mar 11 12:00:12 EST 1995

See here:

icyrock.com
  • 25,648
  • 4
  • 58
  • 78