Skip to content

Commit

Permalink
add formatDuration helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Apr 23, 2024
1 parent 2675a57 commit 4407016
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions jpos/src/main/java/org/jpos/iso/ISODate.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;

/**
Expand Down Expand Up @@ -349,4 +350,21 @@ private static int calculateNearestFullYear(int year, Calendar now) {
}
return possibleYear;
}

public static String formatDuration(Duration d) {
long days = d.toDays();
long hours = d.toHoursPart();
long minutes = d.toMinutesPart();
long seconds = d.toSecondsPart();
StringJoiner sj = new StringJoiner(", ");
if (days > 0)
sj.add(days + "d");
if (hours > 0)
sj.add(hours + "h");
if (minutes > 0)
sj.add(minutes + "m");
if (seconds > 0 || sj.length() == 0)
sj.add(seconds + "s");
return sj.toString();
}
}

0 comments on commit 4407016

Please sign in to comment.