Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:07:03+00:00 2026-06-07T01:07:03+00:00

I must be using the java.util.Calendar#add(…) method wrong as it is giving me unexpected

  • 0

I must be using the java.util.Calendar#add(…) method wrong as it is giving me unexpected results. Assume some initial conditions:

  • I create a Calendar object and initialize it to January 30, 2012.
  • I try to add 47 weeks to it, extract the Date from it and print out the results.
  • I try to add 48 weeks to the original instance (or a cloned copy of it), and print out the results.

I would assume that there would be 7 days difference between the two results, but I’m getting 7 days + 1 year.

For example:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;

public class CalendarFun {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      
      cal.set(Calendar.MONTH, Calendar.JANUARY);
      cal.set(Calendar.DAY_OF_MONTH, 30);
      cal.set(Calendar.YEAR, 2012);
      
      Date date = cal.getTime();
      SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");
      System.out.println(dateFormat .format(date));
      
      Calendar newCal = (Calendar) cal.clone();
      newCal.add(Calendar.WEEK_OF_YEAR, 47);
      System.out.println("add 47 weeks: " + dateFormat.format(newCal.getTime()));

      newCal = (Calendar) cal.clone();
      newCal.add(Calendar.WEEK_OF_YEAR, 48);
      System.out.println("add 48 weeks: " + dateFormat.format(newCal.getTime()));
   }
}

This prints out:

01/30/2012
add 47 weeks: 12/24/2012
add 48 weeks: 12/31/2013

12/31/2013? How can I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:07:07+00:00Added an answer on June 7, 2026 at 1:07 am

    Your code didn’t compile for me unless I changed the YYYY to yyyy. After that it seems to work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a property text=000.01 but using java.util.Properties 's method getProperty("text"); returns "0.01" (must
I've Googled for this but must be using the wrong keywords. Basically I want
I have some code that I absolutely must implement using goto . For example,
At some point I must have moved files from one location to another using
I'm using java.util.regex.Pattern to match passwords that meet the following criteria: At least 7
Here is my code.... package calendar; import java.util.*; /** * This class represents a
I am using java.util.concurrent.ConcurrentSkipListSet . I define a class People whose objects are held
Is there some interface that will allow for iteraction of arrays and java.util.Lists? Seeing
I want to reverse a java.util.LinkedList<Integer> using the available methods. Looking in the methods
I am developing a WPF application that must run using Windows Classic theme. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.