docs: public static String format(Date d, TimeZone tz) {
      if (d == null) throw new IllegalArgumentException("Null date value");
      
      long offset = d.getTime();
      int tzOffsetMs = (tz != null ? tz : LOCAL_TZ).getOffset(d.getTime());
      if (offset > Long.MAX_VALUE - EPOCH_OFFSET_MS - tzOffsetMs) throw new ArithmeticException("Cannot convert to ISO-8601 offset.");
      offset += EPOCH_OFFSET_MS + tzOffsetMs;
      
      long da = offset / MS_IN_DAY;
      int millis = (int)(offset - (da * MS_IN_DAY));
      // Adjust for negative offsets.
      if (millis < 0) {
        da--;
        millis += MS_IN_DAY;
      }
      
      int year = (int)(da * 400 / 146097) + 1; // == da / 365.2425 + 1
      int day = (int)(da - elapsedDays(year)) + 1;
      while (day < 1) day += daysInYear(--year);
      int temp;
      while (day > (temp = daysInYear(year))) {
        day -= temp;
        year++;
      }
      
      int month = FIRST_MONTH;
      while (day > (temp = daysInMonth(year, month))) {
        day -= temp;
        month++;
      }
      
      int hour = millis / MS_IN_HOUR;
      millis -= hour * MS_IN_HOUR;
      int min = millis / MS_IN_MIN;
      millis -= min * MS_IN_MIN;
      int sec = millis / MS_IN_SEC;
      millis -= sec * MS_IN_SEC;
  
      StringBuilder sb = new StringBuilder();
      if (year < 0) {
        sb.append('-');
        year = -year;
      }
      
      append(sb, year, 4);
      sb.append(DATE_SEP);
      append(sb, month, 2);
      sb.append(DATE_SEP);
      append(sb, day, 2);
      sb.append(DATE_TIME_SEP);
      append(sb, hour, 2);
      sb.append(TIME_SEP);
      append(sb, min, 2);
      sb.append(TIME_SEP);
      append(sb, sec, 2);
      
      if (millis > 0) {
        sb.append('.');
        for (int i = 2; i >= 0 && millis > 0; i--) {
          sb.append(millis / TENS[i]);
          millis = millis % TENS[i];
        }
      }
      
      if (tz != null) {
        if (tzOffsetMs == 0) {
          sb.append('Z');
        } else {
          int tzOffset = tzOffsetMs / MS_IN_MIN; // XSD specifies minutes.
          sb.append(tzOffset < 0 ? '-' : '+');
          tzOffset = Math.abs(tzOffset);
          append(sb, tzOffset / MINS_IN_HOUR, 2);
          sb.append(TIME_SEP);
          append(sb, tzOffset % MINS_IN_HOUR, 2);
        }
      }
      return sb.toString();
    }