code: static int parsePeriod(
          String period,
          char symbol
      ) throws ParseException {
  
          if (period.isEmpty()) {
              throw new ParseException("Empty period.", 0);
          }
  
          boolean negative = false;
          int index = 0;
          int len = period.length();
  
          if (period.charAt(0) == '-') {
              index++;
              negative = true;
          }
  
          if ((index < len) && (period.charAt(index) != 'P')) {
              throw new ParseException("Missing P-literal: " + period, index);
          }
  
          index++;
          long total = 0;
          int old = index;
  
          for (int i = index, n = Math.min(len, 10); i < n; i++) {
              char c = period.charAt(i);
              if ((c >= '0') && (c <= '9')) {
                  int digit = (c - '0');
                  total = total * 10 + digit;
                  index++;
              } else {
                  break;
              }
          }
  
          if (index == old) {
              throw new ParseException("Missing digits: " + period, index);
          }
  
          if ((len == index + 1) && (period.charAt(index) == symbol)) {
              index++; // consume unit symbol
              try {
                  if (negative) {
                      total = MathUtils.safeNegate(total);
                  }
                  return MathUtils.safeCast(total);
              } catch (ArithmeticException ae) {
                  throw new ParseException(ae.getMessage(), index);
              }
          }
  
          throw new ParseException("Unparseable format: " + period, index);
  
      }
docs: called by subclasses