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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:46:09+00:00 2026-06-05T22:46:09+00:00

Possible Duplicate: Calculating the Difference Between Two Java Date Instances How do I get

  • 0

Possible Duplicate:
Calculating the Difference Between Two Java Date Instances

How do I get number of days between two dates in Java?

What’s the best way? Here is what I got but it’s not the best:

public static ConcurrentHashMap<String, String> getWorkingDaysMap(int year, 
    int month, int day){
        int totalworkingdays=0,noofdays=0;
        String nameofday = "";
        ConcurrentHashMap<String,String> workingDaysMap = 
            new ConcurrentHashMap<String,String>();
        Map<String,String> holyDayMap = new LinkedHashMap<String,String>();
        noofdays = findNoOfDays(year,month,day);

        for (int i = 1; i <= noofdays; i++) {
            Date date = (new GregorianCalendar(year,month - 1, i)).getTime();
            // year,month,day
            SimpleDateFormat f = new SimpleDateFormat("EEEE");
            nameofday = f.format(date);

            String daystr="";
            String monthstr="";

            if(i<10)daystr="0";
            if(month<10)monthstr="0";

            String formatedDate = daystr+i+"/"+monthstr+month+"/"+year;

            if(!(nameofday.equals("Saturday") || nameofday.equals("Sunday"))){
                workingDaysMap.put(formatedDate,formatedDate);
                totalworkingdays++;
            }
        }

        return workingDaysMap;
    }

public static int findNoOfDays(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days;
    }
  • 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-05T22:46:11+00:00Added an answer on June 5, 2026 at 10:46 pm

    I usually do something like this:

    final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
    
    int diffInDays = (int) ((date1.getTime() - date2.getTime())/ DAY_IN_MILLIS );
    

    No need for external lib, and easy enough


    Update: Just saw you want the “Dates” between too, similar method applies:

    //assume date1 < date2
    
    List<Date> dateList = new LinkedList<Date>();
    for (long t = date1.getTime(); t < date2.getTime() ; t += DAY_IN_MILLIS) {
      dateList.add(new Date(t));
    }
    

    Of course, using JODA time or other lib may make your life a bit easier, though I don’t see the current way difficult to implement


    Update: Important Note!
    this only works for timezone that has no daylight saving or similar adjustment, or your definition of “difference in days” actually means “difference in 24-hour unit”

    • 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 time1: 17:05 time2: 17:08
Possible Duplicate: Calculating the Difference Between Two Java Date Instances hi, I have two
Possible Duplicate: Calculating the Difference Between Two Java Date Instances In Java, I want
Possible Duplicates: Calculating difference in dates in Java How can I calculate a time
Possible Duplicate: Simple statistics - Java packages for calculating mean, standard deviation, etc I
Possible Duplicate: How do you get a timestamp in JavaScript? Calculating milliseconds from epoch
Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: “BigInt” in C? Hey there! I'm calculating the Fibonacci numbers in C
Possible Duplicate: ^ operator in java I was assuming that c ^ d is
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always

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.