|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-10-2008, 10:07 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: Sri Lanka
Posts: 2
|
|
|
[Very Urgent] Need help calculating difference between two dates
I'm trying to write a code to display the current day and time, then accept two dates from the user and display the difference between the two dates. To display the current date and time, I've used the SimpleDateFormatter library but I'm having difficulty calculating the difference between two dates. Could someone please help me with this?
Below is my code so far
import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;
public class DateFormatter {
public void displayNow()
{
Date todaysDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Today's date and time is: "+formattedDate);
}
public void calculateDifference(Date startDate, Date endDate)
{
/* This is whre i need help! */
}
public static void main(String[] args)
{
DateFormatter df = new DateFormatter();
Scanner sc = new Scanner(System.in);
df.displayNow();
System.out.println("Please enter a date: ");
String date1 = sc.next();
System.out.println("Please enter another date: ");
String date2 = sc.next();
}
}
The methods displayNow() and calculateDifference(Date startDate, Date endDate) are essential and cannot be skipped out.
|
|

07-10-2008, 10:51 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
|
Calendar, it's setTime, getTime, and add methods, a counter, and a while loop. Check the API for Calendar and attempt something. Once you have something, post it, and we can help you correct it, but we are not going to do it for you.
|
|

07-10-2008, 03:05 PM
|
|
Member
|
|
Join Date: Jul 2008
Location: Sri Lanka
Posts: 2
|
|
|
for the record, i didn't ask anyone to do it for me!
|
|

07-10-2008, 03:09 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
And I didn't say you did, I just gave the warning that we wouldn't.
|
|

07-10-2008, 06:33 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
public void calculateDifference(Date startDate, Date endDate)
It seems like the above mentioned method should return something. If not, what does it do then?
|
|

07-11-2008, 09:33 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 533
|
|
|
In recent versions, the use of Calendar objects instead of Date objects is preferred for uses like this.
But in either class, the actual date/time value is kept in a long, you can just get the long, and subtract them to get the time difference, and then use an appropriate function (or even just division and mod functions) to convert the milliseconds to days, months, years.
Get the arithmetic right first, and then use DateFormat to display it.
|
|

07-11-2008, 10:11 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
Originally Posted by fishtoprecords
In recent versions, the use of Calendar objects instead of Date objects is preferred for uses like this.
But in either class, the actual date/time value is kept in a long, you can just get the long, and subtract them to get the time difference, and then use an appropriate function (or even just division and mod functions) to convert the milliseconds to days, months, years.
Get the arithmetic right first, and then use DateFormat to display it.
Don't forget to account for the time skew that leads to the leap years. (And that is the exact reason why Calendar is preferred, now. The programmer doesn't need to bother with it.)
|
|

12-11-2008, 03:19 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Difference between two dates in years
Hi all,
I need java code to get differnce between two given dates
|
|

12-11-2008, 03:36 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 551
|
|
|
So write some.
Hint:
Calendar and it's add method. (then you start to hammer out the hour/minute/second differences after you have the "days" difference)
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|