Why are months off by one with Java SimpleDateFormat?

后端 未结 5 967
野趣味
野趣味 2020-12-11 04:53

I am using SimpleDateFormat to display a Calendar like this :

public String getDate()
{
    String DATE_FORMAT = \"EEEE, dd/MM/yyyy HH:mm:ss\";
    SimpleDat         


        
5条回答
  •  孤城傲影
    2020-12-11 05:23

    Unfortunately, months in class Date and class Calendar are zero-based. (In my opinion, this was a huge design mistake in those classes, and it's just one of the many design mistakes in Java's date and time API).

    Note that class Calendar has constants to represent the months: Calendar.JANUARY, Calendar.FEBRUARY etc. Use those instead of the raw numbers.

    An often mentioned, much better date and time API for Java is Joda Time. Note that there is a proposal to add a new date and time API to the next version of Java that will be based on Joda Time.

提交回复
热议问题