• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing time string

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic