集册 Java实例教程 如何使用Calendar类在当前日期和时间值中添加或减去天。

如何使用Calendar类在当前日期和时间值中添加或减去天。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

419
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
如何使用Calendar类在当前日期和时间值中添加或减去天。


import java.util.Calendar;

 

public class Main {
/*
时代Java 提供
*/

 

  public static void main(String[] args) {

    Calendar now = Calendar.getInstance();

   

    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)

                        + "-"

                        + now.get(Calendar.DATE)

                        + "-"

                        + now.get(Calendar.YEAR));

   

    //add days to current date using Calendar.add method/**nowjava**/

    now.add(Calendar.DATE,1);

 

    System.out.println("date after one day : " + (now.get(Calendar.MONTH) + 1)

                        + "-"

                        + now.get(Calendar.DATE)

                        + "-"

                        + now.get(Calendar.YEAR));

 

   

    //subtract days from current date using Calendar.add method

    now = Calendar.getInstance();

    now.add(Calendar.DATE, -10);

 

    System.out.println("date before 10 days : " + (now.get(
展开阅读全文