Java 程式,用於在指定日期中新增給定時間


以下是一個在指定日期中新增給定時間的示例。

程式

現場測試

import java.util.*;

public class Main {
   public static void main(String[] args) throws Exception {
      Date d1 = new Date();
      Calendar cl = Calendar. getInstance();
      cl.setTime(d1);
      System.out.println("today is " + d1.toString());
      cl. add(Calendar.MONTH, 1);
      System.out.println("date after a month will be " + cl.getTime().toString() );
      cl. add(Calendar.HOUR, 70);
      System.out.println("date after 7 hrs will be " + cl.getTime().toString() );
      cl. add(Calendar.YEAR, 3);
      System.out.println("date after 3 years will be " + cl.getTime().toString() );
   }
}

輸出

today is Mon Jun 22 02:47:02 IST 2009
date after a month will be Wed Jul 22 02:47:02 IST 2009
date after 7 hrs will be Wed Jul 22 09:47:02 IST 2009
date after 3 years will be Sun Jul 22 09:47:02 IST 2012

更新於:19-06-2020

92 views

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.