Java的GregorianCalendar的和日历错位周末,天在一个月的八月和九月,2010在一、个月、日历、周末

2023-09-04 06:36:50 作者:朱颜染尘兮丶

我试图使用日历或公历迭代,并创建一个日期格。不过,他们两人似乎认为,2010年8月有30天,而9月2日和3是周末。这是因为如果日历有错误的一年,但我特里普尔核对该参数。下面是一些摘录。

  startDate.set(2010,8,28);

SchedulerCalendar的currentdate =(SchedulerCalendar)startDate.clone();
  的for(int i = 0; I< daysDisplayed;我++){
   newDate =新的TextView(本);
   串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);
  }


 私有类SchedulerCalendar扩展GregorianCalendar的{
  @覆盖
  公共无效添加(INT场,int值){
   super.add(场,值);
   如果(this.get(Calendar.DAY_OF_WEEK)== Calendar.SATURDAY){
    如果(值GT; = 0)super.add(Calendar.DATE,2);
    如果(值小于0)super.add(Calendar.DATE,-1);
   }
   如果(this.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY){
    如果(值GT; = 0)super.add(Calendar.DATE,1);
    如果(值小于0)super.add(Calendar.DATE,-2);
   }
  }
 }
 

解决方案

我强烈怀疑你没有考虑到当月0为基础的事实。所以这样的:

  startDate.set(2010,8,28);
 

时,将其设置为月的2010年28号你还没说你希望它是,但我怀疑你想要月(这是第7个月处于java.util.Calendar中)。

我怀疑这是问题,因为的月的第2个和第3个星期六和星期天的。

我可以强烈建议您使用约达时间的java.util中为您的日期和时间API,而不是{日期,日历}?内置的班有很多陷阱的这个样子。

记录在电脑便签中的内容怎么在电脑桌面显示透明日历清单

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);
   }
  }
 }

解决方案

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.