Why does a new SimpleDateFormat object contain calendar with the wrong year?

后端 未结 5 591
暖寄归人
暖寄归人 2020-12-20 15:54

I came upon a strange behavior that has left me curious and without a satisfactory explanation as yet.

For simplicity, I\'ve reduced the symptoms I\'ve noticed to th

5条回答
  •  抹茶落季
    2020-12-20 16:23

    System.out.println(new SimpleDateFormat().getCalendar());
    System.out.println(new GregorianCalendar());
    

    comparing above code is comparing apples and pears

    The first provides you a tool to parse String into Dates and vice versa The second is a DateUtility that allows you to manipulate Dates

    There is not really a reason why the should provide similar output.

    Compare it with the following

    System.out.println(new String() );
    System.out.println(new Date().toString() );
    

    both lines will output a String but logicly you wouldnt expect the same result

提交回复
热议问题