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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:11+00:00 2026-05-28T07:17:11+00:00

I am generating all dates between two dates. public List<Date> generateRangeDates(Date firstDate, Date lastDate)

  • 0

I am generating all dates between two dates.

public List<Date> generateRangeDates(Date firstDate, Date lastDate) {
    List<Date> allDates = new ArrayList<Date>();
    Calendar calendar = new GregorianCalendar();

    calendar.setTime(firstDate);

    while (calendar.getTime().before(lastDate)) {
        Date resultado = calendar.getTime();
        allDates .add(resultado);
        calendar.add(Calendar.DATE, 1);
    }

    Date lastDate1 = allDates .get(dates.size()-1);
    Calendar cal = new GregorianCalendar();
    cal.setTime(lastDate1);
    cal.add(Calendar.DATE, 1);
    allDates .add(cal.getTime());

    for(Date date1 : dates) {
        System.out.println("All Dates : "+ date1);
    }

    return dates;
}

Above method generating All Dates starting from the first Date till last date. I know it has a major shortcomings. like i could have used joda-time instead of java.util.Calendar. there is also a ambiguity about daylight saving. also it didn’t mention TIMEZONE. Also using GregorianCalendar, which totally ignores the current Locale.
– for the time being – I DO NOT WANT TO USE JODA API.

Help needed to update the above function which works in all TIMEZONES. I have googled a lot on this but also got confused a lot.

also a question ( might sound a bit stupid)-
Why do people insist using JODA API? doesn’t it have any ambiguity?

Note – these lines generating the last date and being added in allDates list.

Date lastDate1 = allDates .get(dates.size()-1);
Calendar cal = new GregorianCalendar();
cal.setTime(lastDate1);
cal.add(Calendar.DATE, 1);
allDates .add(cal.getTime());
  • 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-28T07:17:12+00:00Added an answer on May 28, 2026 at 7:17 am

    Is there any reason not to use long for times? If you need to use Dates you can do this.

    Date is time zone independent.

     public List<Date> generateRangeDates(Date firstDate, Date lastDate) {
          List<Date> dates = new ArrayList<Date>();
          for(long t = firstDate.getTime(); t <= lastDate.getTime(); t += 24 * 60 * 60 * 1000)
              dates.add(new Date(t));
          return dates;
     }
    

    You can’t create a list which is complete TimeZone independent as there is no such data structure in Java.

    The reason other may have suggested using JODA is it have proper Date (without time) objects as it is properly timezone independent.

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

Sidebar

Related Questions

I am generating 10 random floats between 6 and 8 (all for good reason),
How would I go about generating a list of all possible permutations of a
we're currently generating all our official documents using XSL-FO transformation using .xml files as
Is there any command available for generating all missing spec files for existing models
Here's my code: $attendances = $this->Pupil->Attendance->find('all', array( 'conditions'=>array( 'Pupil.group_id'=>$group, 'Attendance.date' >= $start, 'Attendance.date' <=
This is how im generating my date i want to add 1 year to
My PackageMaker project was generating a .mpkg file, but then, all of the sudden,
This is a design question. I find myself going back and forth between two
I have a route defined like this: routes.MapRoute(Date, Date/{year}/{month}/{day}, new { controller = Date,
I’m building a availability calendar using the CodeIgniter’s calendar class. I’m generating checkboxes for

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.