Forums Register Login

Parsing time string

+Pie Number of slices to send: Send
Hi,

I'm in a need of an advice for parsing specific time strings (duration) to get the number of seconds.

The time string can be either

X,Y - number, one or two digit

"Xh Ymn" or "Xmn Ys" or "Xs Yms" (output of mediainfo command line interface)

and I need a way of converting it into seconds or milliseconds.

I though of using regexps for matching different combination an than multiplying the result accordingly but maybe there is a simpler way.
+Pie Number of slices to send: Send
The java.text.SimpleDateFormat class and its various parse methods may help with that.
+Pie Number of slices to send: Send
I've come up with:



but for some reason I have to correct the result by 3600s (or 3600000ms if I dont divide it by 1000) otherwise I get results like:


Anyone knows why it happens ?
+Pie Number of slices to send: Send
Date.getTime returns something that is in terms of the GMT timezone, while the DateFormat classes use the timezone that's the default for where you're based. So I'd guess that you're one hour away from GMT. Try dates like "1h 1mn GMT" with a pattern like "kk'h' mm'mn' z" to accommodate that.

There are probably methods in the Calendar API to find out the current offset from GMT so that you can add/subtract it.
+Pie Number of slices to send: Send
Note that a java.util.Date object represents a specific point in time. It does not represent a duration, and you can't really use java.util.Date objects to store durations. In fact, the standard Java API does not have anything to store time durations at all. Despite what Ulf says, you can't use SimpleDateFormat really well for working with durations.

To work with durations, you can use a third-party library such as the popular open source Joda Time library. You can use the org.joda.time.format.PeriodFormatter class of that library to parse and format strings that represent durations.
+Pie Number of slices to send: Send
Yeah, I understand that might not be the best solution but it seem to be working for now.

I'll check the Joda Time later on, thanks.
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1908 times.
Similar Threads
optimation of resources plz help
Convert seconds to absolute time
how to solve numberformatexception which getting edittext field in android
stacks
Date object
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:34:02.