我有以下代码:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeOneHour));
calendar.set(Calendar.MINUTE,Integer.valueOf(timeOneMinute) );
calendar.set(Calendar.SECOND, 00);
timeOneHour = 10 和timeOneMinute = 20
现在我执行以下操作:
System.out.println("On Day before adding: "+calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH+1)));
System.out.println("On Day after adding: "+calendar.get(Calendar.DAY_OF_MONTH));
以上两个println命令都打印出2 - 我在这里做的错误是什么?
答案 0 :(得分:2)
您是否尝试过像这样增加一天:?
calendar.add(Calendar.DAY_OF_MONTH, 1);
答案 1 :(得分:1)
如果添加了+1,则会出现错误
calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH+1)));
应该是
calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH)+1));
你正在向常数DAY_OF_MONTH添加一个,恰好是DAY_OF_YEAR。一月份他们是一样的。)