• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date and Time

 
Balaji Sivaprakasam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh thanks guys.

sorry for the late reply.

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

thanks once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic