java.util.Calendar add()方法
java.util.Calendar.add() 此方法根据日历的规则将指定的(带符号的)时间量添加到给定的日历字段中。
1 语法
public void add(int field,int amount)
2 参数
field:日历字段。
amount:要添加到该字段的日期或时间的数量。
3 返回值
无
4 示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar add()方法的例子
*/
import java.util.Calendar;
public class CalendaraddExample1 {
public static void main(String[] args) {
// create a new calendar
Calendar cal = (Calendar) Calendar.getInstance();
// print the current date and time
System.out.println("" + cal.getTime());
// add 9 years
cal.add((Calendar.YEAR), 9);
// print the modified date and time
System.out.println("" + cal.getTime());
}
}
输出结果为:
Sun Jul 29 07:42:05 PDT 2018
Thu Jul 29 07:42:05 PDT 2027
5 示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar add()方法的例子
*/
import java.util.Calendar;
import java.util.*;
public class CalanderaddExample2 {
public static void main(String[] args) {
// create a new calendar
Calendar cal = (Calendar) Calendar.getInstance();
// print the current date and time
System.out.println("" + cal.getTime());
// add 2 months
cal.add((Calendar.MONTH), 2);
// print the modified date and time
System.out.println("" + cal.getTime());
}
}
输出结果为:
Sun Jul 29 07:43:38 PDT 2018
Sat Sep 29 07:43:38 PDT 2018
6 示例3
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar add()方法的例子
*/
import java.util.Calendar;
public class CalendaraddExample3 {
public static void main(String[] args){
// create a new calendar
Calendar cal = (Calendar) Calendar.getInstance();
// print the current date and time
System.out.println("" + cal.getTime());
int weekday = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DAY_OF_MONTH, 10);
// print the modified date and time
System.out.println("" + cal.getTime());
}
}
输出结果为:
Sun Jul 29 07:44:56 PDT 2018
Wed Aug 08 07:44:56 PDT 2018
7 示例4
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar add()方法的例子
*/
import java.util.Calendar;
public class CalendaraddExample4 {
public static void main(String[] args){
// create a new calendar
Calendar cal = (Calendar) Calendar.getInstance();
// print the current date and time
System.out.println("" + cal.getTime());
int weekday = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.HOUR, 10);
// print the modified date and time
System.out.println("" + cal.getTime());
cal.add(Calendar.MINUTE, 10);
System.out.println("" + cal.getTime());
cal.add(Calendar.SECOND, 10);
System.out.println("" + cal.getTime());
}
}
输出结果为:
Sun Jul 29 07:46:00 PDT 2018
Sun Jul 29 17:46:00 PDT 2018
Sun Jul 29 17:56:00 PDT 2018
Sun Jul 29 17:56:10 PDT 2018
热门文章
优秀文章