Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Adding or Subtracting Weeks to Current Date in Java

  1. import java.util.Calendar;
  2. public class Weeks {
  3. public static void main(String[] args) {
  4. //create Calendar instance
  5. Calendar now = Calendar.getInstance();
  6. System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
  7. + "-"
  8. + now.get(Calendar.DATE)
  9. + "-"
  10. + now.get(Calendar.YEAR));
  11. System.out.println("Current week of month is : " +
  12. now.get(Calendar.WEEK_OF_MONTH));
  13. System.out.println("Current week of year is : " +
  14. now.get(Calendar.WEEK_OF_YEAR));
  15. //add week to current date using Calendar.add method
  16. now.add(Calendar.WEEK_OF_YEAR,1);
  17. System.out.println("date after one week : " + (now.get(Calendar.MONTH) + 1)
  18. + "-"
  19. + now.get(Calendar.DATE)
  20. + "-"
  21. + now.get(Calendar.YEAR));
  22. //substract week from current date
  23. now =Calendar.getInstance();
  24. now.add(Calendar.WEEK_OF_YEAR,-50);
  25. System.out.println("date before 50 weeks : " + (now.get(Calendar.MONTH) + 1)
  26. + "-"
  27. + now.get(Calendar.DATE)
  28. + "-"
  29. + now.get(Calendar.YEAR));
  30. }
  31. }