1、set设置为某一天:
Calendar cal = Calendar.getInstance();
// cal.setTimeInMillis(time);//假如不设置为当天的话打开注释
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
cal.set(Calendar.MILLISECOND,59);
2、add方法可以对指定日历字段的值进行加减操作,如果第二个参数为正数则加上偏移量,如果为负数则减去偏移量。代码如:
alendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE,-20);//减20分钟
cal.add(Calendar.HOUR_OF_DAY,2);//加两天
3、get获取时间信息
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(year+“年”+month+“月”+day+“日”);