集册 Java实例教程 使用日历在当前时间上加或减分钟

使用日历在当前时间上加或减分钟

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

500
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用日历在当前时间上加或减分钟

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

import java.util.Calendar;

 

public class Main {

 

  public static void main(String[] args) {

   

    Calendar now = Calendar.getInstance();

   

    System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY)

                      + ":"

                      + now.get(Calendar.MINUTE)

                      + ":"

                      + now.get(Calendar.SECOND));

                     

    //add minutes to current date using Calendar.add method

    now.add(Calendar.MINUTE,20);

 

    System.out.println("New time after adding 20 minutes : "

                      + now.get(Calendar.HOUR_OF_DAY)
                      /** from 
                      N o w  J a v a  .   c o m**/

                      + ":"

                      + now.get(Calendar.MINUTE)

                      + ":"

                      + now.get(Calendar.SECOND));

 

    //subtract minutes using Calendar.add method

    now = Calendar.getInstance();

    now.add(Calendar.MINUTE, -150);

 

    System.out.println(
展开阅读全文