Author
DATE/TIME
Norazanita Adnan
Ranch Hand
Joined: May 28, 2008
Posts: 44
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
Joined: Feb 19, 2008
Posts: 2896
My Logic:
Convert date into milliseconds
Subtract them
Convert milliseconds to minutes
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
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
My Website: [Salvin.in ] Cool your mind:[Salvin.in/painting ] My Sally:[Salvin.in/sally ]
Norazanita Adnan
Ranch Hand
Joined: May 28, 2008
Posts: 44
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
Marshal
Joined: Mar 22, 2005
Posts: 35224
posted Apr 05, 2009 10:23:17
0
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
Android apps – ImageJ plugins – Java web charts
Norazanita Adnan
Ranch Hand
Joined: May 28, 2008
Posts: 44
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
Joined: May 28, 2008
Posts: 44
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
Joined: May 28, 2008
Posts: 44
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
Joined: Oct 27, 2005
Posts: 19216
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 :
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Norazanita Adnan
Ranch Hand
Joined: May 28, 2008
Posts: 44
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
Joined: Oct 27, 2005
Posts: 19216
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
Joined: May 28, 2008
Posts: 44
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
Joined: Oct 27, 2005
Posts: 19216
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.
subject: DATE/TIME