Java GregorianCalendar and Calendar misplace weekends, days in a month for August and September, 2010

前端 未结 3 934
星月不相逢
星月不相逢 2021-01-28 18:35

I\'m trying to use either Calendar or Gregorian Calendar to iterate and create a date grid. However, both of them seem to think that August, 2010 has 30 days, and that Septembe

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 19:02

    I strongly suspect you're not taking into account the fact that the month is 0-based. So this:

    startDate.set(2010, 8, 28);
    

    is setting it to September 28th 2010. You haven't said what you expect it to be, but I suspect you wanted August (which is month 7 in java.util.Calendar).

    I suspect this is the problem, given that October 2nd and 3rd are Saturday and Sunday.

    Can I strongly recommend that you use Joda Time as your date and time API instead of java.util.{Date,Calendar}? The built-in classes have lots of gotchas like this.

提交回复
热议问题