count days between dates - easy and very urgent

Posted on 2004-09-16
Last Modified: 2012-06-27
Hi, I just need code that counts the number of days between two dates which are in the following format:

String date1 = "dd-MM-yyyy";
String date2 = "dd-MM-yyyy";

Thank you.
Question by:iAwin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 18

Expert Comment

ID: 12081907
LVL 18

Expert Comment

ID: 12081912
check this link as well
LVL 18

Expert Comment

ID: 12081913
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 92

Accepted Solution

objects earned 200 total points
ID: 12081969
SimpleDateFormat sdf = new SimpleDateFormat("mm-MM-yyyy");
Date d1 = sdf.parse(date1);
Date d2 = sdf.parse(date2);
int days = d2.getTime() - d1.getTime())/1000/60/60/24;

Expert Comment

ID: 12082042
You can use this function...

private long countDays(String date1, String date2)
            String date[] = date1.split("-");
            Calendar d1 = new GregorianCalendar(Integer.parseInt(date[2]), Integer.parseInt(date[1]), Integer.parseInt(date[0]));
            date = date2.split("-");
            Calendar d2 = new GregorianCalendar(Integer.parseInt(date[2]), Integer.parseInt(date[1]), Integer.parseInt(date[0]));
            long diff = d1.getTimeInMillis() - d2.getTimeInMillis();
                  diff = d2.getTimeInMillis() - d1.getTimeInMillis();
            return diff/(24*60*60*1000);

Author Comment

ID: 12198777

Thanks :)

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question