Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Date and Time

Greenhorn
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.

Rancher
Posts: 42975
76
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?

Balaji Sivaprakasam
Greenhorn
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
Rancher
Posts: 42975
76
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
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
Rancher
Posts: 42975
76
Dividing the days by 365.25 (and rounding it off afterwards) might be sufficient to take care of leap years.

Balaji Sivaprakasam
Greenhorn
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
Rancher
Posts: 42975
76
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.

Sheriff
Posts: 21208
87
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.

Balaji Sivaprakasam
Greenhorn
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.

 this is supposed to be a surprise, but it smells like a tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS