Analysis duration string in milliseconds

I need to 98d 01h 23m 45s string of form duration 98d 01h 23m 45s in milliseconds.

I was hoping the equivalent of SimpleDateFormat for such durations, but I could not find anything. Would anyone recommend for or against trying to use SDF for this purpose?

My current plan is to use regex to match 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 think my question is that it seems like busting, can this be done easier? There were a lot of questions about dates and timestamps, but that is a little different, maybe.

+6
source share
3 answers

Using Pattern is the smart way. But why not use one to get all four fields?

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

Then use indexed group sampling.

EDIT:

Based on your idea, I ended up writing 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; } 
+5
source

Check out PeriodFormatter and PeriodParser from JodaTime Library .

You can also use PeriodFormatterBuilder to create a parser for your lines, such as

 String periodString = "98d 01h 23m 45s"; PeriodParser parser = new PeriodFormatterBuilder() .appendDays().appendSuffix("d ") .appendHours().appendSuffix("h ") .appendMinutes().appendSuffix("m ") .appendSeconds().appendSuffix("s ") .toParser(); MutablePeriod period = new MutablePeriod(); parser.parseInto(period, periodString, 0, Locale.getDefault()); long millis = period.toDurationFrom(new DateTime(0)).getMillis(); 

Now all this (especially the toDurationFrom(...) ) may seem complicated, but I really advise you to take a look at JodaTime if you are dealing with periods and duration in Java.

Also look at this answer about getting milliseconds from a JodaTime period for further clarification.

+11
source

The new java.time.Duration class in Java 8 allows you to analyze duration out of the box:

 Duration.parse("P98DT01H23M45S").toMillis(); 

the format is a little different, so adjustment will be required to parse it.

+4
source

Source: https://habr.com/ru/post/918019/


All Articles