I have a string which comes from an API and it gives me the duration of a trip. Sometimes it is look like this : PT5H27M sometimes it is like : PT5H and sometime it is like : PT27M
I would like to get a Date variable from this. I wrote some function by using pattern and matcher but it takes too long when my list of trips are long. Would you suggest me a better way. I attached my code in the following:
final Pattern p = Pattern.compile("PT(\\d*)H*(\\d*)M*");
private long getDurationOfTrip(Pattern pattern,String duration){
long durationOfTrip = 0 ;`enter code here`
Matcher m = pattern.matcher(duration);
if (m.find()){
if (duration.contains("H")&&duration.contains("M"))
durationOfTrip = (Long.parseLong(m.group(1))*60) + Long.parseLong(m.group(2));
else if (duration.contains("H")&& !duration.contains("M"))
durationOfTrip = (Long.parseLong(m.group(1))*60);
else
durationOfTrip = Long.parseLong(m.group(1));
}
return durationOfTrip;
}
Aucun commentaire:
Enregistrer un commentaire