How to Add Or Subtract Minutes from Current Date in Java


In this example, we will write an example to add or substract minutes from current time with Java Calendar class.

Source Code

package com.beginner.examples;

import java.util.Calendar;

public class ASMinutesExample {

    public static void main(String[] args){
    	//get calendar
    	Calendar now = Calendar.getInstance();
    	System.out.println(now.get(Calendar.MINUTE));
		//minutes add 1
    	now.add(Calendar.MINUTE, 1);
		System.out.println(now.get(Calendar.MINUTE));
		//minutes subtract 1
		now.add(Calendar.MINUTE, -1);
		System.out.println(now.get(Calendar.MINUTE));
	}
}

Output:

38
39
38

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments