Returns the suffix or "units" of the duration as a string. The result will be ms, s, m, h, d, w, n or y. @param duration The duration in the format #units, e.g. 1d or 6h @return Just the suffix, e.g. 'd' or 'h' @throws IllegalArgumentException if the duration is null, empty or if the units are inval
(final String duration)
| 240 | * @since 2.4 |
| 241 | */ |
| 242 | public static final String getDurationUnits(final String duration) { |
| 243 | if (duration == null || duration.isEmpty()) { |
| 244 | throw new IllegalArgumentException("Duration cannot be null or empty"); |
| 245 | } |
| 246 | int unit = 0; |
| 247 | while (unit < duration.length() && |
| 248 | Character.isDigit(duration.charAt(unit))) { |
| 249 | unit++; |
| 250 | } |
| 251 | final String units = duration.substring(unit).toLowerCase(); |
| 252 | if (units.equals("ms") || units.equals("s") || units.equals("m") || |
| 253 | units.equals("h") || units.equals("d") || units.equals("w") || |
| 254 | units.equals("n") || units.equals("y")) { |
| 255 | return units; |
| 256 | } |
| 257 | throw new IllegalArgumentException("Invalid units in the duration: " + units); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Parses the prefix of the duration, the interval and returns it as a number. |