集册 Java实例教程 使用日历向当前日期添加或减去年份

使用日历向当前日期添加或减去年份

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

598
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用日历向当前日期添加或减去年份

 

import java.util.Calendar;/*来 自 N o  w  J a v a . c o m - 时  代  Java*/

 

public class Main {

 

  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 year to current date using Calendar.add method

    now.add(Calendar.YEAR,1);

   /*from nowjava.com - 时  代  Java*/

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

                        + "-"

                        + now.get(Calendar.DATE)

                        + "-"

                        + now.get(Calendar.YEAR));

   

    //subtract year from current date

    now =Calendar.getInstance();

    now.add(Calendar.YEAR,-100);

    System.out.println("date before 100 years : " + (now.g
展开阅读全文