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
LVL 18

Expert Comment

ID: 12081907
LVL 18

Expert Comment

ID: 12081912
check this link as well
LVL 18

Expert Comment

ID: 12081913
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 72
javap not working 8 46
JavaFX TableView not displaying correctly 3 19
going to wrong jsp page 2 22
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 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