• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DATE/TIME

 
Norazanita Adnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...

need some idea or sample codes on this issue

fromActivityDate/Time : 2009-03-13 08:07:00.0
toActivityDate/Time : 2009-03-14 12:33:00.0

from this Date/Time.. how can i fine the total minute from the From and to Date/Time? at least give me the logic how it work...

i been "googeling" over the net..i cant find something similar ...

i know it sound easy but i dont have the idea how can i do..hope sumone able to help me with this

thanks in advance

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Logic:

Convert date into milliseconds
Subtract them
Convert milliseconds to minutes
 
salvin francis
Bartender
Posts: 1309
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are inbuilt classes that can convert a string to a date, you have to specify the format

use this to convert both values into objects and once you have your two date objects, use Sagar Rohankar's method

 
Norazanita Adnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any sample code as a guidance for me? it really help me a lot if i can have some samples codes...
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't much code to it if you follow the steps Sagar lists. Do you have the dates/times as Date objects or in some other formats?

Some generic date-handling code is in the http://faq.javaranch.com/java/JavaDatesFaq
 
Norazanita Adnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i have this sample..how can i set the date to what i want?
i think think code is the solution to my problem right?
 
Norazanita Adnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the format

fromActivityDate/Time : 2009-03-13 08:07:00.0
toActivityDate/Time : 2009-03-14 12:33:00.0

 
Norazanita Adnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've solve the first part...
now just left with Convert milliseconds to minutes.. thanks so much all of you man!...
i will just Google and see how to Convert milliseconds to minutes...

thanks so much..

code ranch rock!
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divide by 1000 to get seconds, then by 60 to get minutes comes to mind.

Or, when using Java 6, use java.util.concurrent.TimeUnit:
 
Norazanita Adnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks bob your solution works..but im a bit confuse and doubt where my code goes wrong.
i try your first solution


the result is 0

why is it so..but the i used
it works well as it give me the value...

it just that i cast it to String as i want my return type to be Sting..or i have to ght the value first then only i can turn to String it is?

anyway..the problem solve..thanks so much...
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the value of secondTry? Perhaps elapsed_time / 60000 is a multiple of 60? Because TimeUnit.MILLISECONDS.toMinutes actually just divides by 60000.
 
Norazanita Adnan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4320...it is the right output as the total days is 3days...

its ok..i just the 2nd solution that you gave..it work ..

thanks once again...
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one worked as well, because 4320 % 60 == 0. I would still prefer the second solution though, since it makes it much clearer what you want to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic