Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Android Calendar.set方法在第一次修改尝试时给出错误的结果?_Java_Android_Calendar - Fatal编程技术网

Java 为什么Android Calendar.set方法在第一次修改尝试时给出错误的结果?

Java 为什么Android Calendar.set方法在第一次修改尝试时给出错误的结果?,java,android,calendar,Java,Android,Calendar,我正在Android上创建自己的日历UI实现。其功能之一是通过简单地将日历的月值增加或减少1来更改当前查看的月历 默认日历值是使用以下命令初始化的: this.currentCalendar = Calendar.getInstance(Locale.US); 下面是更改currentcendarmonth值的onClick侦听器实现 @Override public void onClick(View v) { int month = this.currentCalendar.get

我正在Android上创建自己的日历UI实现。其功能之一是通过简单地将日历的月值增加或减少1来更改当前查看的月历

默认日历值是使用以下命令初始化的:

this.currentCalendar = Calendar.getInstance(Locale.US);
下面是更改
currentcendar
month值的onClick侦听器实现

@Override
public void onClick(View v) {
    int month = this.currentCalendar.get(Calendar.MONTH);
    int year = this.currentCalendar.get(Calendar.YEAR);
    SimpleDateFormat sdf = new SimpleDateFormat("MMM yyyy", Locale.US);     
    switch(v.getId()) {
        case R.id.calendarNextMonthButton: // Next Button Clicked
            month++;
            break;
        case R.id.calendarPrevMonthButton: // Prev Button Clicked
            month--;
            break;
    }
    this.currentCalendar.set(Calendar.MONTH, month);
    Log.d("month", String.valueOf(this.currentCalendar.get(Calendar.MONTH)));
    this.monthYearText = (TextView) this.v.findViewById(R.id.calendarMonthYearText);
    this.monthYearText.setText(sdf.format(this.currentCalendar.getTime()));
}
初始化完成后,日历将正确显示
currentCalendar
month和year值,例如month=0(一月),year=2014。 当我第一次单击下一步按钮时,
month
值增加1
currentCalendar
month值通过以下方式设置:

this.currentCalendar.set(Calendar.MONTH, month); // debugger says month is 1
但是,当我试图显示
currentCalendar
的月份值时,调试器会说月份值是2(三月),而不是1(二月)。这仅在第一次尝试单击“下一步”按钮时发生。下次单击“下一步”和“上一步”按钮时,日历月份发生了完美的变化

代码有什么问题吗

PS:我正在使用java.util.Calendar作为
currentCalendar

的日期?我假设今天(29日)是您现在要求的时间,并使用
getInstance()
获取
日历
。。。这意味着这是正确的;因为二月只有28天,所以加上一个月的话,它就转到三月了

从:

Calendar有两种解释日历字段的模式,即宽松模式和非宽松模式。当日历处于宽松模式时,它接受的日历字段值的范围比它产生的范围更广。当日历重新计算get()返回的日历字段值时,所有日历字段都将被规范化。例如,宽大的GregorianCalendar将月份==1月,月中日==32解释为2月1日


一旦发生这种情况。。。正如你所说,一切都很完美,因为从2月29日到3月1日,每个月的第1天已经正常化了

是的,今天是2014年1月29日。哇,我没想到。在更改月份之前,我将当前日历的日期值设置为1。现在它工作得很好。谢谢你为我节省了这么多时间。@Aryo很高兴我能帮上忙。您还可以使用
.add()
.roll()
进行更细粒度的控制-查看Javadoc中主要描述的最后一段。