|
| |||||||||
| Tags: class, date, days, integer, java, object, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Program to calculate days difference between two dates in java.
Hello friends, I am last year B.Sc.I.T. student. I recently started learning java language. In our last lecture our sir has given one program like "Write a Program to calculate days difference between two dates in java", but none of us able to write right program. That's why I am asking this question on this forum. Any one know how to write Program to calculate days difference between two dates in java. Please help me. Thanks in advanced. |
|
#2
| ||||
| ||||
| Re: Program to calculate days difference between two dates in java.
In the following example we are going to calculate the differences between two dates. For this you have to first converts the actual date value into the corresponding date / time value in milliseconds and then do the math so we can get the different in days, hours, minutes, etc. Code: package org.kodejava.example.util;
import java.util.Calendar;
public class DD
{
public static void main(String[] args)
{
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.set(2006, 12, 30);
c2.set(2007, 5, 3);
long milis1 = c1.getTimeInMillis();
long milis2 = c2.getTimeInMillis();
long d = milis2 - milis1;
long dSeconds = diff / 1000;
long dM = d / (60 * 1000);
long dH = d / (60 * 60 * 1000);
long dD = d / (24 * 60 * 60 * 1000);
System.out.println("In milliseconds: " + d + " milliseconds.");
System.out.println("In seconds: " + dS + " seconds.");
System.out.println("In minutes: " + dM + " minutes.");
System.out.println("In hours: " + dH + " hours.");
System.out.println("In days: " + dD + " days.");
}
} |
|
#3
| ||||
| ||||
| Re: Program to calculate days difference between two dates in java.
You have to use setTimeInMillis(long millis) method to set current time in calendar object and you have to use getInstance() method to get a calendar using the default time zone, locale and current time. Code: import java.util.Calendar;
public class DateDifferent{
public static void main(String[] args){
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.set(2010, 01, 21);
ca2.set(2010, 10, 21);
long m1 = calendar1.getTimeInMillis();
long m2 = calendar2.getTimeInMillis();
long d = m2 - m1;
long dS = d / 1000;
long dM = d / (60 * 1000);
long dH = d / (60 * 60 * 1000);
long dD = d / (24 * 60 * 60 * 1000);
System.out.println("\nThe Date Different Example");
System.out.println("Time in milliseconds: " + d
+ " m.");
System.out.println("Time in seconds: " + dS
+ " seconds.");
System.out.println("Time in minutes: " + dM
+ " minutes.");
System.out.println("Time in hours: " + dH
+ " hours.");
System.out.println("Time in days: " + dD
+ " days.");
}
}
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| Re: Program to calculate days difference between two dates in java.
Hey it very easy process to calculate days difference between two days. I had written one example of it. Just go through it. Code: import java.util.Date;
import java.util.GregorianCalendar;
public class DateDifference {
public static void main(String[] av) {
Date da1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();
Date today1 = new Date();
long diff1 = today1.getTime() - da1.getTime();
System.out.println("The year (up to " + today + ") is "
+ (diff1 / (1000 * 60 * 60 * 24)) + " the days old.");
}
}
__________________ The FIFA Manager 2009 PC Game |
|
#5
| ||||
| ||||
| Re: Program to calculate days difference between two dates in java.
Hey you have to use following code in your program to calculate days difference between two days. Code:
public static long daysBetween(Calendar sDate, Calendar eDate) {
Calendar d = (Calendar) sDate.clone();
long dBetween = 0;
while (d.before(eDate)) {
d.add(Calendar.DAY_OF_MONTH, 1);
dBetween++;
}
return dBetween;
}
12. } |
|
#6
| |||
| |||
| Re: Program to calculate days difference between two dates in java.
Lol! It is blatantly obvious that you are a UOM student. I find it hard to believe that final year students have hardly touched java, even out of pure interest in programming. A search on google heads plenty of results which do exactly what you want as well. ![]() |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Program to calculate days difference between two dates in java." | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to Calculate Expiration Dates in Excel | Kungfu Pandey | Windows Software | 1 | 07-01-2012 04:22 PM |
| Auto calculate the amount of stays according to selected dates | Macario | Software Development | 4 | 17-11-2009 10:13 PM |
| Calculate duration rather than # of days between two dates | SianH | Microsoft Project | 3 | 06-10-2009 08:13 AM |
| Calculating difference in days between two dates in Asp.Net | Jagriti | Software Development | 3 | 08-07-2009 12:23 AM |
| How to calculate two dates in Excel | Ektaa | Windows Software | 5 | 03-03-2009 05:50 PM |