Why are months off by one with Java SimpleDateFormat?

后端 未结 5 961
野趣味
野趣味 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.

    0 讨论(0)
  • 2020-12-11 05:25

    that's your locale set to france DD/MM/YY so it is tuesday, July, 2010. the 6th month is july if it starts at 0.

    0 讨论(0)
  • 2020-12-11 05:32

    month index starts from 0 just like array index

    0 讨论(0)
  • 2020-12-11 05:33

    From the Java API:

    public int getMonth()

    Returns a number representing the month that contains or begins with the instant in time represented by this Date object. The value returned is between 0 and 11, with the value 0 representing January.

    0 讨论(0)
  • 2020-12-11 05:36

    the getMonth method in Date is 0 indexed. from the JavaDoc:

    Returns a number representing the month that contains or begins with the instant in time represented by this Date object. The value returned is between 0 and 11, with the value 0 representing January.

    0 讨论(0)
提交回复
热议问题