Joda Time

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?
[139 byte] By [blackmagea] at [2007-11-27 10:09:51]
# 1

> 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)

~

yawmarka at 2007-7-13 0:46:11 > top of Java-index,Java Essentials,Java Programming...
# 2
Sorry, I've not heard of Joda time. I've heard of estoy jodido time, but that's entirely different.
petes1234a at 2007-7-13 0:46:11 > top of Java-index,Java Essentials,Java Programming...
# 3
> Sorry, I've not heard of Joda time.https://jsr-310.dev.java.net/~
yawmarka at 2007-7-13 0:46:11 > top of Java-index,Java Essentials,Java Programming...
# 4
> https://jsr-310.dev.java.net/Thanks! I'll look it up.
petes1234a at 2007-7-13 0:46:11 > top of Java-index,Java Essentials,Java Programming...
# 5

> > 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.

blackmagea at 2007-7-13 0:46:11 > top of Java-index,Java Essentials,Java Programming...
# 6

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?

blackmagea at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

pbrockway2a at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...
# 8
> Sorry, I've not heard of Joda time. I've heard ofcount yourself lucky, it's a disaster to work with.
jwentinga at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...
# 9
> count yourself lucky, it's a disaster to work with.so then it really IS el tiempe que estoy jodido.
petes1234a at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...
# 10

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?

blackmagea at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...
# 11
> where does the 1000 come from?all our time are in milliseconds
roger.bagnalla at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...
# 12

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);

?

blackmagea at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...
# 13
Yep.~
yawmarka at 2007-7-13 0:46:12 > top of Java-index,Java Essentials,Java Programming...