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?
@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
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot!
Also for 8hrs workday calendar I did:
("d", 8* 60 * 60)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.