为Java中的Calendar类导入以下包import java.util.Calendar;
首先,创建一个Calendar对象并显示当前日期和时间Calendar calendar = Calendar.getInstance();
System.out.println(“Current Date and Time = ” + calendar.getTime());
现在,让我们使用calendar.add()方法和Calendar.WEEK_OF_YEAR常量增加周数。calendar.add(Calendar.WEEK_OF_YEAR, 2);
以下是一个例子
示例import java.util.Calendar;
public class Demo {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println(“Current Date = ” + calendar.getTime());
//加2周
calendar.add(Calendar.WEEK_OF_YEAR, 2);
System.out.println(“Updated Date = ” + calendar.getTime());
}
}
输出结果Current Date = Thu Nov 22 18:13:38 UTC 2018
Updated Date = Thu Dec 06 18:13:38 UTC 2018