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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:11:16+00:00 2026-05-20T16:11:16+00:00

I am working with the Java Calendar class to do the following: Set a

  • 0

I am working with the Java Calendar class to do the following:

  1. Set a start date
  2. Set an end date
  3. Any date within that range is a “valid” date

I have this somewhat working, and somewhat not. Please see the code below:

    nowCalendar.set(Calendar.DATE, nowCalendar.get(Calendar.DATE) + offset);
    int nowDay = nowCalendar.get(Calendar.DATE);

    Calendar futureCalendar = Calendar.getInstance();
    futureCalendar.set(Calendar.DATE, nowDay + days);

    Date now = nowCalendar.getTime();
    Date endTime = futureCalendar.getTime();

    long now_ms = now.getTime();
    long endTime_ms = endTime.getTime();

    for (; now_ms < endTime_ms; now_ms += MILLIS_IN_DAY) {
        valid_days.addElement(new Date(now_ms));
        System.out.println("VALID DAY: " + new Date(now_ms));
    }

Basically, I set a “NOW” calendar and a “FUTURE” calendar, and then I compare the two calendars to find the valid days. On my calendar, valid days will be shaded white and invalid days will be shaded gray. You will notice two variables:

    offset = three days after the current selected date
    days = the number of valid days from the current selected date

This works…EXCEPT when the current selected date is the last day of the month, or two days prior (three all together). I think that its the offset that is definitely screwing it up, but the logic works everywhere else. Any ideas?

  • 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-20T16:11:17+00:00Added an answer on May 20, 2026 at 4:11 pm

    Don’t fiddle with milliseconds. Clone the nowCalendar, add 1 day to it using Calendar#add() in a loop as long as it does not exceed futureCalendar and get the Date out of it using Calendar#getTime().

    Calendar clone = nowCalendar.clone();
    
    while (!clone.after(futureCalendar)) {
        validDays.add(clone.getTime());
        clone.add(Calendar.DATE, 1);
    }
    

    (note that I improved validDays to be a List instead of the legacy Vector)

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

Sidebar

Related Questions

I have 2 date object in the database that represent the company's working hours.
I'm working on a java program, and I have several vectors defined and filled
I'm working with a Java program that has multiple components (with Eclipse & Ant
I am working Calendar API (Java). My specific requirement is I want to add
I have been working on a BlackBerry application that consumes web services from ColdFusion
Can someone show me a piece of java code that parses this date: 2009-08-05
I have a question about working with time in java, more specificly in Android.
I'm working in Java with XML and I'm wondering; what's the difference between an
I am working in Java on a fairly large project. My question is about
I recently started working in Java and was introduced to the wild and crazy

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.