wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Date and Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date and Time" Watch "Date and Time" New topic
Author

Date and Time

Balaji Sivaprakasam
Greenhorn

Joined: Jan 13, 2008
Posts: 8
Hi buddies,

am Vishva and this is the first topic i have posted in javaranch.

hope you all will help me.

am in a beginner level in java.
so i need your help.
help me for this question.
how can we find the time difference between two given dates.
i know that we can find the milliseconds.

but is there any methods to find exact difference?

for ex:
05 Jan 2008
07 Jan 2008

difference is 2 days and if i have given any time in the sense it should give the hours and minutes difference also.

help me to find the solution.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Welcome to JavaRanch.

Well, if you know the difference in milliseconds, then it's not hard to figure out the difference in days, is it?


Ping & DNS - my free Android networking tools app
Balaji Sivaprakasam
Greenhorn

Joined: Jan 13, 2008
Posts: 8
no dear i don't know about that.

i told you know i am a beginner.

so tell me how to convert it into days.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
If you have the milliseconds between two dates, then the days are

days = milliseconds / 1000 / 60 / 60 / 24

Some rounding would be necessary to account for daylight savings time (when there are 23 or 25 hours in a day).

A more full-featured example can be found here.
Balaji Sivaprakasam
Greenhorn

Joined: Jan 13, 2008
Posts: 8
Great. thanks amigo[friend in latin]
i got the formula to convert the milliseconds to days. the program also working correct and it gives me some idea. now tell me if i try to convert those days into years then what will happen? we have to check the condition for the leap year also?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Dividing the days by 365.25 (and rounding it off afterwards) might be sufficient to take care of leap years.
Balaji Sivaprakasam
Greenhorn

Joined: Jan 13, 2008
Posts: 8
now i got it.

Thanks dear.

i have to learn a lot about java because i have to write SCJP with in two months. can you give me any idea. what should i refer?

any links for mock exams, java materials, tutorials?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
I'm sure you've seen the very active SCJP forum. We also have an extensive SCJP FAQ and a lengthy list of mock exams. These should provide you with enough hints to get going.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

If you use java.util.Calendar class, you can calculate the difference more precisely even with things as daylight savings time.

This will work only if the years are the same. If they aren't, you can correct the value by using Calendar.add(Calendar.YEAR, 1) and Calendar.getActualMaximum(Calendar.DAY_OF_YEAR). The latter will return either 365 or 366, dependant of the year.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Balaji Sivaprakasam
Greenhorn

Joined: Jan 13, 2008
Posts: 8
oh thanks guys.

sorry for the late reply.

now i got some clear picture about what i have to do.

thanks once again.
 
 
subject: Date and Time
 
Similar Threads
Passed SCJP 1.4 : What's next
Final Class Problem
Contemporary Salaries
Date validation with XSD
SCEA 4 part -II