GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Dividing Time into equal intervals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dividing Time into equal intervals" Watch "Dividing Time into equal intervals" New topic
Author

Dividing Time into equal intervals

kittu shusma
Greenhorn

Joined: Apr 18, 2012
Posts: 14
Hi,

i have given start time and end time,
the difference between start time and end time i got some x time,
i have to split that time into 50 min segments of timesolts.
for example...
8.30 - 12.00 = 3.30,
this 3.30 min can be divided into 50 min of timesolts like this format,
8.30
9.20
10.10
11.00
11.50....
can you help to me solve this problem.

Thanks,
Kittushusma.


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

kittu shusma wrote:i have given start time and end time,
the difference between start time and end time i got some x time,
i have to split that time into 50 min segments of timesolts.

I'm not quite sure what you're asking. A Java Date (which can be obtained from a Java GregorianCalendar) is simply a number of milliseconds from the a fixed point in time (called the 'epoch'). You can therefore get the difference between two Dates in milliseconds, after which it's simple a matter of division to get the minutes.

Does that help?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

you need to change 3.30 to 210 minutes


SCJP
Visit my download page
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
What does deducting start time from
end time have to do with anything?

startTime + 50mins = newTime
newTime + 50mins = anotherNewTime
repeat until endTime
kittu shusma
Greenhorn

Joined: Apr 18, 2012
Posts: 14
i just given the start time,end time and class duration..
can you please send to me that logic...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> can you please send to me that logic...

most of it is in the earlier post.

start by creating a calendar object.
set the time to the start time
then (in a loop)
a) add 50 minutes
b) check if the new time is after the end time
if not
{
c) save the time/print it out/whatever
repeat above, in the loop
}
if it is after the end time
{
do nothing
break out of the loop
}

have a go, if you get stuck, post what you've tried.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
What format are you storing those times in? When you write 8.30, how is it stored?
kittu shusma
Greenhorn

Joined: Apr 18, 2012
Posts: 14
I tried but am not getting proper out put,
can you please send me the code..
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

kittu shusma wrote:I tried but am not getting proper out put,
can you please send me the code..


Hi Kittu, that generally isn't how things work here (We are NotACodeMill). What we would prefer is that you show us your code which you tried to implement the above suggestions and tell us the part where you are having trouble. Then we can help you with the problems. So, can you show us your code?


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
What sort of format have you got those “times” in? Are they floating-point numbers? If so, you will find it dreadfully difficult to get the correct times from them.
kittu shusma
Greenhorn

Joined: Apr 18, 2012
Posts: 14
Hi,

I tried but am not getting proper out put,
i am sending the code is there any mistakes please correct me..

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
well, that code is a bit of a dog's breakfast.

why would you create all those things in each loop of a for loop?

to start, your SimpleDateFormat pattern is wrong - read what each part represents, you'll find the error

use 2 calendars start/end, setting them to the times you have
then, in a while() loop (not a for()), you check while(!startCal.after(endCal))//allows it to be equal,
and do 2 things (1) add to the arraylist (2) add 50 minutes.

when you have it working, you'll see how simple it is
 
GeeCON Prague 2014
 
subject: Dividing Time into equal intervals