Java的GregorianCalendar的和日历错位周末

Java的GregorianCalendar的和日历错位周末

本文介绍了Java的GregorianCalendar的和日历错位周末,天在一个月的八月和九月,2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用日历或公历迭代,并创建一个日期格。不过,他们两人似乎认为,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.

这篇关于Java的GregorianCalendar的和日历错位周末,天在一个月的八月和九月,2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:56