日期:2014-05-20  浏览次数:21314 次

java中利用Calendar获得月份的时候为什么要加1?
package C;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class GetTime {
public static void main(String []args)
{
Date taday=new Date(); //获取当前日期和时间
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
String dateString = formatter.format(taday);
System.out.println("当前时间为:"+dateString);

long currentTime=System.currentTimeMillis(); //当前时间的毫秒数
 
Date tomorrow=new Date(currentTime+24*60*60*1000);
System.out.println("明天是"+tomorrow.toString());
 
Calendar now=Calendar.getInstance();
int year=now.get(Calendar.YEAR);
int month=now.get(Calendar.MONTH)+1;//?为什么要加1
int day=now.get(Calendar.DATE);
System.out.println("今天是"+year+"年"+month+"月"+day+"日");

int week=now.get(Calendar.DAY_OF_WEEK);
switch(week)
{
case 1:
System.out.println("星期日");
break;
case 2:
System.out.println("星期一");
break;
case 3:
System.out.println("星期二");
break;
case 4:
System.out.println("星期三");
break;
case 5:
System.out.println("星期四");
break;
case 6:
System.out.println("星期五");
break;
case 7:
System.out.println("星期六");
break;
case 8:
System.out.println("星期日");
break;
}
}
}


------解决方案--------------------
Calendar.MONTH =0 Calendar的月份是从 0开始算的, 所以要+1.
------解决方案--------------------
探讨
Calendar.MONTH =0 Calendar的月份是从 0开始算的, 所以要+1.

------解决方案--------------------
Calendar月份从一月到十二月对应的值是0 - 11所以都要+1的啊