Files
javaSE-0113/javaSE-day01/src/com/inmind/calendar03/Demo10.java

36 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.calendar03;
import java.util.Calendar;
/*
12.Calendar中的add方法
能够对时间进行计算
public abstract void add(int field, int amount) :根据日历的规则,为给定的日历字段添加或减去指定的时间量。
field参数
指定的年月日常量
amount参数:
正数:增加
负数:减少
*/
public class Demo10 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
showCalendar(calendar);
//在当前时间的基础上+2年
calendar.add(Calendar.YEAR,2);
showCalendar(calendar);
//在当前时间的基础上+10月
calendar.add(Calendar.MONTH,10);
showCalendar(calendar);
//在当前时间的基础上-10天
calendar.add(Calendar.DAY_OF_MONTH,10);
showCalendar(calendar);
}
public static void showCalendar(Calendar calendar) {
System.out.println(calendar.get(Calendar.YEAR)+""+(calendar.get(Calendar.MONTH)+1)+""+calendar.get(Calendar.DAY_OF_MONTH)+"");
}
}