Parses the prefix of the duration, the interval and returns it as a number. E.g. if you supply "1d" it will return "1". If you supply "60m" it will return "60". @param duration The duration to parse in the format #units, e.g. "1d" or "60m" @return The interval as an integer, regardless of units. @th
(final String duration)
| 268 | * @since 2.4 |
| 269 | */ |
| 270 | public static final int getDurationInterval(final String duration) { |
| 271 | if (duration == null || duration.isEmpty()) { |
| 272 | throw new IllegalArgumentException("Duration cannot be null or empty"); |
| 273 | } |
| 274 | if (duration.contains(".")) { |
| 275 | throw new IllegalArgumentException("Floating point intervals are not supported"); |
| 276 | } |
| 277 | int unit = 0; |
| 278 | while (Character.isDigit(duration.charAt(unit))) { |
| 279 | unit++; |
| 280 | } |
| 281 | int interval; |
| 282 | try { |
| 283 | interval = Integer.parseInt(duration.substring(0, unit)); |
| 284 | } catch (NumberFormatException e) { |
| 285 | throw new IllegalArgumentException("Invalid duration (number): " + duration); |
| 286 | } |
| 287 | if (interval <= 0) { |
| 288 | throw new IllegalArgumentException("Zero or negative duration: " + duration); |
| 289 | } |
| 290 | return interval; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Returns whether or not a date is specified in a relative fashion. |