> Is there anyone who is familiar with Joda Time and
> knows how to convert a time like 5:13 (5 minutes and
> 13 seconds) to milliseconds?
Check the Period and Duration classes in the API.
#! /usr/bin/groovy
import org.joda.time.*
duration = new Period(0, 5, 13, 0).toDurationFrom(new Instant())
millis = DateTimeUtils.getDurationMillis(duration)
assert millis == (5 * 60 * 1000) + (13 * 1000)
~
> > Is there anyone who is familiar with Joda Time
> and
> > knows how to convert a time like 5:13 (5 minutes
> and
> > 13 seconds) to milliseconds?
>
> Check the Period and Duration classes in the API.
>
> #! /usr/bin/groovy
> import org.joda.time.*
>
> duration = new Period(0, 5, 13, 0).toDurationFrom(new
> Instant())
> millis = DateTimeUtils.getDurationMillis(duration)
> assert millis == (5 * 60 * 1000) + (13 *
> 1000)
>
> ~
Ok, I playing around with it like so
public ConvertTime(){
int time;
period=new Period(0,5,23,0);
Duration duration = new Period(0, 5, 13, 0).toDurationFrom(new Instant());
long millis = DateTimeUtils.getDurationMillis(duration);
System.out.println(millis);
assert millis == (5 * 60 * 1000) + (13 * 1000);
//System.out.println(period.);
}
But what is the point of the last line with assert? And what does assert do? Never heard of it before.
And I can turn thing back into real time by
duration = new Period(0, 5, 13, 1).toDurationFrom(new Instant());
millis = DateTimeUtils.getDurationMillis(duration);
System.out.println(millis);
period=new Period(millis);
System.out.println(period.getMinutes());
System.out.println(period.getSeconds());
System.out.println(period.getMillis());
Butt still, what does assert do?
> Butt still, what does assert do?
Read all about them: http://java.sun.com/javase/6/docs/technotes/guides/language/assert.html
Basically they go "bang!" if the condition is false. Since yawmark knew what millis was intended to be, the assertion provides a simple check that the preceeding couple of lines had what done what was wanted.
Duration duration = new Period(0, 5, 13, 0).toDurationFrom(new Instant());
long millis = DateTimeUtils.getDurationMillis(duration);
System.out.println(millis);
assert millis == (5 * 60 * 1000) + (13 * 1000);
So 5 comes from the minutes, 60 is how many minutes in an hour or seconds in a minute, I'm guessing, but where does the 1000 come from?
Ok, say the code was:
Duration duration = new Period(0, 5, 13, 24).toDurationFrom(new Instant());
Where 24 was the number of milliseconds. Would assert then be:
assert millis == (5 * 60 * 1000) + (13 * 1000)+(24);
?