Return the proper Calendar time unit as an integer given the string @param units The unit to parse @return An integer matching a Calendar.<UNIT> enum @throws IllegalArgumentException if the unit is null, empty or doesn't match one of the configured units. @since 2.3
(final String units)
| 619 | * @since 2.3 |
| 620 | */ |
| 621 | public static int unitsToCalendarType(final String units) { |
| 622 | if (Strings.isNullOrEmpty(units)) { |
| 623 | throw new IllegalArgumentException("Units cannot be null or empty"); |
| 624 | } |
| 625 | |
| 626 | final String lc = units.toLowerCase(); |
| 627 | if (lc.equals("ms")) { |
| 628 | return Calendar.MILLISECOND; |
| 629 | } else if (lc.equals("s")) { |
| 630 | return Calendar.SECOND; |
| 631 | } else if (lc.equals("m")) { |
| 632 | return Calendar.MINUTE; |
| 633 | } else if (lc.equals("h")) { |
| 634 | return Calendar.HOUR_OF_DAY; |
| 635 | } else if (lc.equals("d")) { |
| 636 | return Calendar.DAY_OF_MONTH; |
| 637 | } else if (lc.equals("w")) { |
| 638 | return Calendar.DAY_OF_WEEK; |
| 639 | } else if (lc.equals("n")) { |
| 640 | return Calendar.MONTH; |
| 641 | } else if (lc.equals("y")) { |
| 642 | return Calendar.YEAR; |
| 643 | } |
| 644 | throw new IllegalArgumentException("Unrecognized unit type: " + units); |
| 645 | } |
| 646 | |
| 647 | } |