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

前端 未结 3 968
星月不相逢
星月不相逢 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:17

    As others have already mentioned it might be some of the common "off by one" errors this library design helps to make. The Java API is more or less some wrapping of a UNIX timestamp largely inspired by some C library.

    You should at least use the appropriate Enumerations to improve readability and make sure to not pass Date/Calendar objects to other methods which could work with some immutable representation (String, Long, ...) of the data depending on the use case.

    If you are heavily involved with Times, Dates and Calendars you should consider using JodaTime and look into JSR 310, the Date and Time API which might appear in a future Java release.

提交回复
热议问题