Java中Calendar对于日期的控制详解--DAY_OF_MONTH, DAY_OF_YEAR, DATE 的区别
package action;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TestCalendar {
public static void getCaltime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println("当前系统日期:"+sdf.format(cal.getTime()));
cal.add(Calendar.DATE,1);
System.out.println(",1:"+sdf.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH,1);
System.out.println("Calendar.DAY_OF_MONTH:"+sdf.format(cal.getTime()));
cal.add(Calendar.DAY_OF_YEAR,1);
System.out.println("Calendar.DAY_OF_YEAR:"+sdf.format(cal.getTime()));
System.out.println("这一天在是这个月的第多少天:"+cal.get(Calendar.DAY_OF_MONTH)); //用来获得这一天在是这个月的第多少天
System.out.println("这一天在是这个年的第多少天:"+cal.get(Calendar.DAY_OF_YEAR));//用来获得这一天在是这个年的第多少天。
cal.set(2018, 7, 8);// 月份是从0开始的,所以11表示12月
System.out.println("set方法设置年月日:"+sdf.format(cal.getTime()));
cal.add(Calendar.YEAR, -1); // 年份减1
System.out.println(", -1:"+sdf.format(cal.getTime()));
cal.add(Calendar.YEAR, +1); // 年份加1
System.out.println(", +1:"+sdf.format(cal.getTime()));
cal.add(Calendar.MONTH, -1);// 月份减1
System.out.println(", -1:"+sdf.format(cal.getTime()));
cal.add(Calendar.DATE, -1);// 日期减1
System.out.println(", -1:"+sdf.format(cal.getTime()));
cal.set(Calendar.HOUR, 15);//设置时为 15点
System.out.println(", 15,“时间从0点开始”:"+sdf.format(cal.getTime()));
cal.set(Calendar.MINUTE, 45);//设置分为 45
System.out.println(", 45:"+sdf.format(cal.getTime()));
cal.set(Calendar.SECOND, 30);//设置 秒为30
System.out.println(", 30:"+sdf.format(cal.getTime()));
}
public static void main(String[] args) {
getCaltime();
}
}