Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Parse millis from timeString

Gleb Kartashov
Contributor
May 19, 2020

Hi!

I have a Time to SLA "TTS - Duration Field" custom field which contains a "timestring" value type. I need to convert this duration to millis, but Date.parse doesn't work for me:

Message:

java.text.ParseException: Unparseable date: "2d"

 How do I convert this type of time to millis?

2 answers

1 accepted

0 votes
Answer accepted
Tuncay Senturk
Community Champion
May 19, 2020

Hi @Gleb Kartashov 

@Nic Brough -Adaptavist- is right this is time string.

Considering you are using 7/24 calendar, below code might help.

Bear in mind that it returns as seconds but you can multiply by 1000 to get milliseconds.

 

import java.util.regex.Matcher;
import java.util.regex.Pattern;


Map timesAsSeconds = new HashMap<String, BigDecimal>();
timesAsSeconds.put("m", 60);
timesAsSeconds.put("h", 60 * 60);
timesAsSeconds.put("d", 24 * 60 * 60);

BigDecimal sum = new BigDecimal(0);
String duration = "3h 30m";
Matcher m = Pattern.compile("(\\d+)([d|h|m]*)").matcher(duration);
while (m.find()) {
String type = m.group(3);
String multiplier = m.group(2);
BigDecimal value = timesAsSeconds.get(type);
sum = sum.add(value.multiply(new BigDecimal(multiplier)));
}

return sum.longValue();

 

I hope it helps 

Tuncay

Tuncay Senturk
Community Champion
May 19, 2020

Also keep in mind that this code returns long value for the given duration "3h 30m".

You need to change this to 

String duration = issue.getCustomFieldValue...

Gleb Kartashov
Contributor
May 19, 2020

Thanks a lot!

Also for 8hrs workday calendar I did:

("d", 8* 60 * 60)

 

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 19, 2020

This looks like an Atlassian "pretty" time string, something Atlassian does, not Java.  To parse these, you'll need to use Atlassian's code, or write your own to convert it.

The format for Atlassian's strings are #w #d #h #m - # being "integer number of" and then week/day/hour/minute.  A parser for those isn't too hard to convert into milliseconds, although you may need to start thinking about working time (for example, a week is 7 days of 24 hours of 60 minutes of 60 seconds of 1000 milliseconds each.  But that's elapsed time, and your SLAs may be working off 1w of 5d of 8h working time)

Gleb Kartashov
Contributor
May 19, 2020

What I'm trying to do is just compare workingDuration variable to max resolution which is stored in "Duration field" in this "pretty time string" format. workingDuration is only incremented during working hours and I don't have to do anything regarding work calendars.

Also tested #w #d #h #m format parsing and got same results:

image.pngimage.png

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 19, 2020

You're missing the point - the #d thing is not a Java function.  Java's "parse" functions won't work with them.

You will need to either translate the strings in your code, or pass them through Atlassian code that understands the format.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events