Java GregorianCalendar 和 Calendar 错位了 2010 年 8 月和 9 月的周末、一个月中的日期

发布于 2024-09-18 12:14:47 字数 1105 浏览 7 评论 0原文

我正在尝试使用日历或公历来迭代并创建日期网格。然而,他们俩似乎都认为2010年8月有30天,9月2日和3日是周末。就好像日历的年份错误,但我已经对该参数进行了三次检查。以下是一些摘录。

startDate.set(2010, 8, 28);

SchedulerCalendar currentDate = (SchedulerCalendar) startDate.clone(); 
  for(int i = 0; i<daysDisplayed; i++){ 
   newDate = new TextView(this); 
   String dateString = currentDate.get(Calendar.MONTH)+"/"+currentDate.get(Calendar.DATE);
   //+"/"+currentDate.get(Calendar.YEAR);
   newDate.setText(dateString);
   newDate.setId(i+1);
   newDate.setWidth(blockWidth);
   newDate.setHeight(DATE_HEIGHT);
   dateBar.addView(newDate);
   currentDate.add(Calendar.DATE, 1);
  }


 private class SchedulerCalendar extends GregorianCalendar {
  @Override
  public void add(int field, int value) {
   super.add(field, value);
   if(this.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY) {
    if(value>=0) super.add(Calendar.DATE, 2);
    if(value<0) super.add(Calendar.DATE, -1);
   }
   if(this.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY) {
    if(value>=0) super.add(Calendar.DATE, 1);
    if(value<0) super.add(Calendar.DATE, -2);
   }
  }
 }

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 September 2 and 3 are weekends. It's as if the calendar has the wrong year, but I've tripple-checked that parameter. Here are some excerpts.

startDate.set(2010, 8, 28);

SchedulerCalendar currentDate = (SchedulerCalendar) startDate.clone(); 
  for(int i = 0; i<daysDisplayed; i++){ 
   newDate = new TextView(this); 
   String dateString = currentDate.get(Calendar.MONTH)+"/"+currentDate.get(Calendar.DATE);
   //+"/"+currentDate.get(Calendar.YEAR);
   newDate.setText(dateString);
   newDate.setId(i+1);
   newDate.setWidth(blockWidth);
   newDate.setHeight(DATE_HEIGHT);
   dateBar.addView(newDate);
   currentDate.add(Calendar.DATE, 1);
  }


 private class SchedulerCalendar extends GregorianCalendar {
  @Override
  public void add(int field, int value) {
   super.add(field, value);
   if(this.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY) {
    if(value>=0) super.add(Calendar.DATE, 2);
    if(value<0) super.add(Calendar.DATE, -1);
   }
   if(this.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY) {
    if(value>=0) super.add(Calendar.DATE, 1);
    if(value<0) super.add(Calendar.DATE, -2);
   }
  }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽袖吟 2024-09-25 12:14:47

我强烈怀疑您没有考虑到月份是从 0 开始的这一事实。所以这样:

startDate.set(2010, 8, 28);

将其设置为 2010 年 9 月 28 日。您还没有说出您期望的日期,但我怀疑您想要 8 月(在 java.util.Calendar 中是第 7 个月)。

我怀疑这就是问题所在,因为10 月 2 日和 3 日是星期六和星期日。

我可以强烈建议您使用 Joda Time 作为日期和时间 API 而不是 java.util.{Date ,日历}?内置类有很多这样的问题。

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.

与往事干杯 2024-09-25 12:14:47

如果您想继续使用java.util.Calendar,您可能还应该使用Calendar.JANUARYCalendar.FEBRUARY等。常数。

If you want to continue to use java.util.Calendar, you should probably also use the Calendar.JANUARY, Calendar.FEBRUARY, etc. constants.

舟遥客 2024-09-25 12:14:47

正如其他人已经提到的,这可能是该库设计导致的一些常见的“相差一”错误。 Java API 或多或少是对 UNIX 时间戳的某种包装,很大程度上受到某些 C 库的启发。

您至少应该使用适当的枚举来提高可读性,并确保不将日期/日历对象传递给其他方法,这些方法可以根据用例处理数据的某些不可变表示(字符串、长整型……)。

如果您对时间、日期和日历有很多了解,您应该考虑使用 JodaTime 并查看 JSR 310,日期和时间 API 可能会出现在未来的 Java 版本中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文