Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:16:09+00:00 2026-06-05T19:16:09+00:00

I need to parse a duration string, of the form 98d 01h 23m 45s

  • 0

I need to parse a duration string, of the form 98d 01h 23m 45s into milliseconds.

I was hoping there was an equivalent of SimpleDateFormat for durations like this, but I couldn’t find anything. Would anyone recommend for or against trying to use SDF for this purpose?

My current plan is to use regex to match against numbers and do something like

Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher("98d 01h 23m 45s");

if (m.find()) {
    int days = Integer.parseInt(m.group());
}
// etc. for hours, minutes, seconds

and then use TimeUnit to put it all together and convert to milliseconds.

I guess my question is, this seems like overkill, can it be done easier? Lots of questions about dates and timestamps turned up but this is a little different, maybe.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:16:10+00:00Added an answer on June 5, 2026 at 7:16 pm

    Using a Pattern is a reasonable way to go. But why not use a single one to get all four fields?

    Pattern p = Pattern.compile("(\\d+)d\\s+(\\d+)h\\s+(\\d+)m\\s+(\\d+)s");
    

    Then use the indexed group fetch.

    EDIT:

    Building off of your idea, I ultimately wrote the following method

    private static Pattern p = Pattern
            .compile("(\\d+)d\\s+(\\d+)h\\s+(\\d+)m\\s+(\\d+)s");
    
    /**
     * Parses a duration string of the form "98d 01h 23m 45s" into milliseconds.
     * 
     * @throws ParseException
     */
    public static long parseDuration(String duration) throws ParseException {
        Matcher m = p.matcher(duration);
    
        long milliseconds = 0;
    
        if (m.find() && m.groupCount() == 4) {
            int days = Integer.parseInt(m.group(1));
            milliseconds += TimeUnit.MILLISECONDS.convert(days, TimeUnit.DAYS);
            int hours = Integer.parseInt(m.group(2));
            milliseconds += TimeUnit.MILLISECONDS
                    .convert(hours, TimeUnit.HOURS);
            int minutes = Integer.parseInt(m.group(3));
            milliseconds += TimeUnit.MILLISECONDS.convert(minutes,
                    TimeUnit.MINUTES);
            int seconds = Integer.parseInt(m.group(4));
            milliseconds += TimeUnit.MILLISECONDS.convert(seconds,
                    TimeUnit.SECONDS);
        } else {
            throw new ParseException("Cannot parse duration " + duration, 0);
        }
    
        return milliseconds;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse this string into three different components: Location: 1|#69.83623|#24.432223|#Cupertino, California The
I need to parse a Python-generated datetime string into a Javascript Date object. I
I need parse through a file and do some processing into it. The file
Hi I need parse and deserialize pseudo JSON string. Input data: {aBubbleData[ 'jaja2581' ]={
I need to parse a string which contains custom tags like [link][description][link-url][/link] and I
I need to parse expressions of the following form: (S (A (B (D xyz))
I need parse a string inside a parenthesis, which looks like (A, B, C),
I need to parse the $_SERVER['argv'] array into a single array. I can decide
I need to parse sentences from a paragraph in Python. Is there an existing
I need to parse XML string with Linq, and I came up with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.