How to Add Or Subtract Weeks in Java


Here we will write an example to add or substract weeks in current date and time values with java Calendar class.

Source Code

package com.beginner.examples;

import java.util.Calendar;

public class ASWeeksExample {

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

Output:

22
23
22

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments