Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2008, 10:07 AM
Member
 
Join Date: Jul 2008
Location: Sri Lanka
Posts: 2
rustix is on a distinguished road
Send a message via MSN to rustix Send a message via Yahoo to rustix Send a message via Skype™ to rustix
[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

Code:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-10-2008, 10:51 AM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-10-2008, 03:05 PM
Member
 
Join Date: Jul 2008
Location: Sri Lanka
Posts: 2
rustix is on a distinguished road
Send a message via MSN to rustix Send a message via Yahoo to rustix Send a message via Skype™ to rustix
for the record, i didn't ask anyone to do it for me!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-10-2008, 03:09 PM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
And
Quote:
for the record
I didn't say you did, I just gave the warning that we wouldn't.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-10-2008, 06:33 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
public void calculateDifference(Date startDate, Date endDate)

It seems like the above mentioned method should return something. If not, what does it do then?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-11-2008, 09:33 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-11-2008, 10:11 AM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
Quote:
Originally Posted by fishtoprecords View Post
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.)
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-11-2008, 03:19 PM
Member
 
Join Date: Jul 2008
Posts: 20
suprabha is on a distinguished road
Difference between two dates in years
Hi all,
I need java code to get differnce between two given dates
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-11-2008, 03:36 PM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Compare two Dates Java Tip java.util 1 06-24-2008 09:05 AM
No fo days between two dates Java Tip Java Tips 0 01-28-2008 11:06 AM
Comparing dates Java Tip Java Tips 0 01-28-2008 11:02 AM
Inbetween Dates Rageagainst20 New To Java 4 12-19-2007 07:24 AM
help with dates and time osval New To Java 3 12-12-2007 02:41 PM


All times are GMT +3. The time now is 12:11 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org