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 6581953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:13:07+00:00 2026-05-25T16:13:07+00:00

I’m trying to write a simple procedure that will add to a List every

  • 0

I’m trying to write a simple procedure that will add to a List every second Monday (every pay period day) since the beginning of the year, what is happening is kind of strange but I’ve never worked with the Calendar class before and I think I may be using compareTo incorrectly but I read the javadoc and it seems okay.
package javasampleapps;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author DavidH
 */
public class AddingDaysToCalendar {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Calendar cal = new GregorianCalendar();
        System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        System.out.println(cal.get(Calendar.WEEK_OF_YEAR));

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.WEEK_OF_YEAR, 1 + 1);

        Calendar currentDate = new GregorianCalendar();
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        int counter = 2;
        List<Date> dates = new ArrayList<Date>();
        while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
            cal.set(Calendar.WEEK_OF_YEAR, counter);
            dates.add(cal.getTime());
            counter += 2;
        }
        System.out.println("Listing Dates:");
        for (Date d : dates) {
            System.out.println(d);
        }
    }
}

It is outputting this:

39
40
Listing Dates:
Mon Jan 03 13:37:29 AST 2011
Mon Jan 17 13:37:29 AST 2011
Mon Jan 31 13:37:29 AST 2011
Mon Feb 14 13:37:29 AST 2011
Mon Feb 28 13:37:29 AST 2011
Mon Mar 14 13:37:29 ADT 2011
Mon Mar 28 13:37:29 ADT 2011
Mon Apr 11 13:37:29 ADT 2011
Mon Apr 25 13:37:29 ADT 2011
Mon May 09 13:37:29 ADT 2011
Mon May 23 13:37:29 ADT 2011
Mon Jun 06 13:37:29 ADT 2011
Mon Jun 20 13:37:29 ADT 2011
Mon Jul 04 13:37:29 ADT 2011
Mon Jul 18 13:37:29 ADT 2011
Mon Aug 01 13:37:29 ADT 2011
Mon Aug 15 13:37:29 ADT 2011
Mon Aug 29 13:37:29 ADT 2011
Mon Sep 12 13:37:29 ADT 2011
Mon Sep 26 13:37:29 ADT 2011

Why is it adding the very last date? Sep 26th hasn’t even occurred yet.

  • 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-25T16:13:07+00:00Added an answer on May 25, 2026 at 4:13 pm

    Why is it adding the very last date? Sep 26th hasn’t even occurred yet.

    Because you advance the date after you check its relation to the current time, and before you add it to the list:

     while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
         cal.set(Calendar.WEEK_OF_YEAR, counter);
         dates.add(cal.getTime());
         counter += 2;
     }
    

    This should fix that problem (may introduce others though):

     while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
         dates.add(cal.getTime());
         counter += 2;
         cal.set(Calendar.WEEK_OF_YEAR, counter);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is 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.