Add or subtract minutes to current time using Calendar - Java Date Time

Java examples for Date Time:Calendar

Description

Add or subtract minutes to current time using Calendar

Demo Code

 
import java.util.Calendar;
 
public class Main {
 
  public static void main(String[] args) {
   //ww  w . j av  a2 s.com
    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)
                      + ":"
                      + 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("Time before 50 minutes : " + now.get(Calendar.HOUR_OF_DAY)
                      + ":"
                      + now.get(Calendar.MINUTE)
                      + ":"
                      + now.get(Calendar.SECOND));
   
  }
}

Result


Related Tutorials