• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing time string  RSS feed

 
Aleksander Braula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.text.SimpleDateFormat class and its various parse methods may help with that.
 
Aleksander Braula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aleksander Braula
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!