stanfordnlp/CoreNLP · error · IllegalArgumentException

Invalid date time zone offset

Error message

Invalid date time zone offset ${str}

What it means

A parse failure in TimeFormatter.parseOffsetMillis: the string is expected to be a UTC-offset expression starting with '+' or '-' (or matching the zero-offset text); anything else, such as a bare offset without a sign, is rejected as an invalid date-time zone offset.

Solutions

  1. Normalize "Z" to "+00:00" (or "-00:00") before handing the string to the parser.
  2. Ensure the offset always carries an explicit sign: prepend "+" if absent and the string starts with a digit.
  3. Pre-validate with a regex like ^[+-]\d{2}(:?\d{2})?$ before invoking.

Example fix

// before
String tz = "Z";
formatter.parseOffset(tz); // throws

// after
String tz = "Z".equals(raw) ? "+00:00" : raw;
formatter.parseOffset(tz);
Defensive patterns

Strategy: validation

Validate before calling

static boolean hasSign(String s) { return s != null && (s.startsWith("+") || s.startsWith("-")); }

Try / catch

try {
  offset = parseOffset(str);
} catch (IllegalArgumentException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Invalid date time zone offset")) {
    if (str != null && str.startsWith("Z")) str = "+00:00";
    else if (str != null && str.matches("\\d.*")) str = "+" + str;
    offset = parseOffset(str);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the TimeFormatter offset-parse path (e.g. via time zone parsing in a formatter) with a string like "08:00", "Z", "GMT+8", or an empty string — missing the required sign prefix.

Common situations: Passing "Z" (UTC designator) where a signed numeric offset is expected; stripping the '+' sign when cleaning timestamps; Excel/spreadsheet data that drops leading '+'.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/2e211805bd9a56e8. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/time/TimeFormatter.java:499

      }
      return sb;
    }

    private static int parseInteger(String str, int pos, int length) {
      return Integer.parseInt(str.substring(pos, pos+length));
    }

    public int parseOffsetMillis(String str) {
      int offset = 0;
      if (zeroOffsetParseText != null && str.equalsIgnoreCase(zeroOffsetParseText)) {
        return offset;
      }
      boolean negative = false;
      if (str.startsWith("+")) {
      } else if (str.startsWith("-")) {
        negative = true;
      } else {
        throw new IllegalArgumentException("Invalid date time zone offset " + str);
      }
      int pos = 1;
      // Parse hours
      offset += DateTimeConstants.MILLIS_PER_HOUR * parseInteger(str, pos, 2);
      pos += 2;
      if (pos < str.length()) {
        // Parse minutes
        if (!Character.isDigit(str.charAt(pos))) { pos++; }
        offset += DateTimeConstants.MILLIS_PER_MINUTE * parseInteger(str, pos, 2);
        pos += 2;
        if (pos < str.length()) {
          // Parse seconds
          if (!Character.isDigit(str.charAt(pos))) { pos++; }
          offset += DateTimeConstants.MILLIS_PER_SECOND * parseInteger(str, pos, 2);
          pos += 2;
          if (pos < str.length()) {
            // Parse fraction of seconds
            if (!Character.isDigit(str.charAt(pos))) { pos++; }

View on GitHub (pinned to 1b7edd19c4)