Calendar calculation
In this chapter you will learn:
- Add minutes to Calendar
- Subtract months to current date
- Add or subtract seconds to current time
- Add and subtract year from Calendar
- Calendar class adjusts the date when adding or subtracting months
Add minutes to Calendar
Calendar class automatically adjust the date or hour when adding or subtracting minutes
import java.util.Calendar;
// j a v a 2s . c o m
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Current time : ");
System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":"
+ now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
// Calendar class automatically adjust the date or hour when adding or subtracting minutes
now.add(Calendar.MINUTE, 200);
System.out.println("After adding 200 minutes:");
System.out.println(now.get(Calendar.HOUR_OF_DAY) + ":"
+ now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
}
}
The output:
Subtract months to current date
We can subtract months to current date by adding minus value.
import java.util.Calendar;
//from j a va 2 s .c om
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
// add months to current date using Calendar.add method
now.add(Calendar.MONTH, 1);
System.out.println("adding one month : ");
System.out.println((now.get(Calendar.MONTH) + 1));
// adding minus value
now = Calendar.getInstance();
now.add(Calendar.MONTH, -1);
System.out.println("adding minus value: ");
System.out.println((now.get(Calendar.MONTH) + 1));
}
}
The output:
Add or subtract seconds to current time
import java.util.Calendar;
/*from ja v a 2 s. c o m*/
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
// add seconds to current date
now.add(Calendar.SECOND, 500);
System.out.println("adding 500 seconds : ");
System.out.println(now.get(Calendar.MINUTE) + ":"
+ now.get(Calendar.SECOND));
// adding minus value
now = Calendar.getInstance();
now.add(Calendar.SECOND, -500);
System.out.println("adding -500 seconds: ");
System.out.println(now.get(Calendar.MINUTE) + ":"
+ now.get(Calendar.SECOND));
}
}
The output:
Add and subtract year from Calendar
import java.util.Calendar;
/* j a v a 2 s . c om*/
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
now.add(Calendar.YEAR, 1);
// add minus value
now = Calendar.getInstance();
now.add(Calendar.YEAR, -1);
}
}
Calendar class auto adjusting
Calendar class automatically adjusts the date when adding or subtracting months.
import java.util.Calendar;
//from j a v a 2s. c o m
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
// adding 100 months to current date
now.add(Calendar.MONTH, 100);
System.out.println("adding one hundred month : ");
System.out.println((now.get(Calendar.MONTH) + 1));
System.out.println(now.get(Calendar.DATE));
System.out.println(now.get(Calendar.YEAR));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What are the methods available for comparing two Calendar objects
- Compare date time using after method
- Compare date time using before method
Home » Java Tutorial » Date, Time, Calendar, TimeZone