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

  • Home
  • SEARCH
  • 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 6152873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:59:43+00:00 2026-05-23T19:59:43+00:00

I have the following code in a JUnit test, which seemed to work last

  • 0

I have the following code in a JUnit test, which seemed to work last week is failing this week:

Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.JULY, 12);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); // push the date to 15
System.out.println(cal.get(Calendar.DATE));

As you could probably infer from my comment, since the 12th is a Tuesday, I expect Date to be 15 after setting the DAY_OF_WEEK to Friday. However, the value that is printed is 22, and causes the test to fail.

If I, however change the code as follows, and add an additional call to get:

Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.JULY, 12);
System.out.println(cal.get(Calendar.DATE));
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); // push the date to 15
System.out.println(cal.get(Calendar.DATE));

I get the output that I expect, 12 and 15.

Can someone explain what is going on, and why this test was working last week?

  • 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-05-23T19:59:44+00:00Added an answer on May 23, 2026 at 7:59 pm

    The first thing to understand is that Month + Day + DayOfWeek does not mean anything to the Calendar. The Calendar will calculate the true value of the date based on

    YEAR + MONTH + DATE

    or

    YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK

    (Or some other combos like year + day of year etc.) So Date + DayOfWeek doesn’t inherently mean much to it.

    The second thing to understand is when you set on a Java Calendar it doesn’t actually recompute the absolute time or update related fields until an operation that forces computation occurs.

    After your first set, the calendar is in a conflicted state. The month and day say that it’s July 12th, but the ‘week of month’ and ‘day of week’ still say that it’s today, whatever today is. You then call set day of week to friday. So now year month and day say July 12th, but the ‘week of month’ and ‘day of week’ fields say it’s Friday of ‘this’ week.

    The rules of the calendar say that the most recently set field “wins” when there’s a conflict, so the week of month and day of week combining to say Friday of this week are what’s used to calculate the other fields.

    Inserting a get in the middle ‘fixes’ it because it forces the entire internal state of the calendar to get recomputed to Tuesday July 12th before setting to Friday, so there are no internal conflicts. The ‘week of month’ got set to the week that contains July 12th by the recalculation prior to you setting day of week to Friday.

    Edit: Sorry to make changes after two days, noticed this open in an old browser tab and thought I would expand for the hopeful help of future googlers:

    The reason it worked for Jon in the comments is he lives in London. His computer thinks weeks start on Mondays. So when asked for Friday of ‘this’ week, it still responded July 15th when asked on Sunday July 17th. I bring this up because differing first days of the week in different Locales are just yet another way that trying to use the WEEK_OF fields in a calendar goes haywire.

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

Sidebar

Related Questions

I have the following test case in eclipse, using JUnit 4 which is refusing
I have following Code Block Which I tried to optimize in the Optimized section
I have the following JUnit test. The method that I'm testing is quite simple,
I am new to Java and junit. I have the following peice of code
I have following code, which executes on button press. At first it works as
I have following code which works fine in iPhone application but in iPad I
i have following code in which, i am fetching the data from the sqlite
I have the following code and I can't figure out why it won't work:
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following code in my application: // to set tip - photo in

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.