Solved

# count days between dates - easy and very urgent

Posted on 2004-09-16
309 Views
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.
0
Question by:iAwin
[X]
###### 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

LVL 18

Expert Comment

ID: 12081907
0

LVL 18

Expert Comment

ID: 12081912
http://www.faqs.org/qa/qa-10170.html
0

LVL 18

Expert Comment

ID: 12081913
0

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;
0

LVL 3

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();
if(d1.before(d2))
diff = d2.getTimeInMillis() - d1.getTimeInMillis();

return diff/(24*60*60*1000);
}
0

Author Comment

ID: 12198777

Thanks :)
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
###### Suggested Courses
Course of the Month5 days, 2 hours left to enroll