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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:07:55+00:00 2026-06-09T05:07:55+00:00

Calculating the difference between two dates ( java.util.Date ) in terms of no. of

  • 0

Calculating the difference between two dates (java.util.Date) in terms of no. of days look like very simple and we can find different ways to do that. I used the following code to calculate the date difference:

  public static long daysBetween(Calendar startDate, Calendar endDate) {
        Calendar date = (Calendar) startDate.clone();
        long daysBetween = 0;
        while (date.before(endDate)) {
            date.add(Calendar.DAY_OF_MONTH, 1);
            daysBetween++;
        }
        return daysBetween;
    }

In main(), I used the following two dates :

    Calendar c1 = Calendar.getInstance();
    c1.set(2011, 1, 1);
    Calendar c2 = Calendar.getInstance();
    c2.set(2011, 1, 31);

    long difference = daysBetween(c1, c2); // 

But the value of the variable difference is not consistent. It is sometimes 30 and sometimes 31. So, why that might have happened.

Is there any solution to use the method results a consistent output ?

  • 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-09T05:07:57+00:00Added an answer on June 9, 2026 at 5:07 am

    You’re setting the date part of the calendars, but not the time part.

    Sometimes the clock will tick between the calls to getInstance() and sometimes it won’t, hence the inconsistency.

    Options:

    • Set the time as well as the date, e.g. to midnight
    • Use a better date/time library – Joda Time – which has a more suitable representation (LocalDate). An important moral here is that if you can find a type which represents the exact information you have, and nothing else, that’s likely to be a good fit and cause fewer complications.

    Using LocalDate, you wouldn’t even have to do the loop as Joda Time has good support for computing the differences between two values anyway.

    LocalDate date1 = new LocalDate(2011, 1, 1);
    LocalDate date2 = new LocalDate(2011, 1, 31);
    Days period = Days.daysBetween(days1, days2);
    int days = period.getDays();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Calculating the Difference Between Two Java Date Instances How do I get
Possible Duplicate: Calculating the Difference Between Two Java Date Instances hi, I have two
Possible Duplicate: Calculating the Difference Between Two Java Date Instances time1: 17:05 time2: 17:08
Possible Duplicate: Calculating the Difference Between Two Java Date Instances In Java, I want
I want to find out the difference between two Dates (in terms of minutes)
Simple one here. I am calculating the percentage difference between two values. I want
Possible Duplicates: Calculating difference in dates in Java How can I calculate a time
I am calculating the difference between two sql dates, TripStartDate and TripEndDate . If
I was doing a simple calculation to get the difference between two dates. If
How would you go about calculating the amount of months between two arbitrary dates?

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.