public static Calendar parseCalendar()

in core/src/main/java/com/datastax/dse/driver/internal/core/search/DateRangeUtil.java [75:167]


  public static Calendar parseCalendar(String source) throws ParseException {
    // The contents of this method are based on Lucene's DateRangePrefixTree#parseCalendar, released
    // under the Apache License, Version 2.0.
    // Following is the original notice from that file:

    // Licensed to the Apache Software Foundation (ASF) under one or more
    // contributor license agreements.  See the NOTICE file distributed with
    // this work for additional information regarding copyright ownership.
    // The ASF licenses this file to You under the Apache License, Version 2.0
    // (the "License"); you may not use this file except in compliance with
    // the License.  You may obtain a copy of the License at
    //
    //     http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.

    if (source == null || source.isEmpty()) {
      throw new IllegalArgumentException("Can't parse a null or blank string");
    }

    Calendar calendar = newCalendar();
    if (source.equals("*")) {
      return calendar;
    }
    int offset = 0; // a pointer
    try {
      // year & era:
      int lastOffset =
          (source.charAt(source.length() - 1) == 'Z') ? source.length() - 1 : source.length();
      int hyphenIdx = source.indexOf('-', 1); // look past possible leading hyphen
      if (hyphenIdx < 0) {
        hyphenIdx = lastOffset;
      }
      int year = Integer.parseInt(source.substring(offset, hyphenIdx));
      calendar.set(Calendar.ERA, year <= 0 ? 0 : 1);
      calendar.set(Calendar.YEAR, year <= 0 ? -1 * year + 1 : year);
      offset = hyphenIdx + 1;
      if (lastOffset < offset) {
        return calendar;
      }

      // NOTE: We aren't validating separator chars, and we unintentionally accept leading +/-.
      // The str.substring()'s hopefully get optimized to be stack-allocated.

      // month:
      calendar.set(
          Calendar.MONTH,
          Integer.parseInt(source.substring(offset, offset + 2)) - 1); // starts at 0
      offset += 3;
      if (lastOffset < offset) {
        return calendar;
      }
      // day:
      calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(source.substring(offset, offset + 2)));
      offset += 3;
      if (lastOffset < offset) {
        return calendar;
      }
      // hour:
      calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(source.substring(offset, offset + 2)));
      offset += 3;
      if (lastOffset < offset) {
        return calendar;
      }
      // minute:
      calendar.set(Calendar.MINUTE, Integer.parseInt(source.substring(offset, offset + 2)));
      offset += 3;
      if (lastOffset < offset) {
        return calendar;
      }
      // second:
      calendar.set(Calendar.SECOND, Integer.parseInt(source.substring(offset, offset + 2)));
      offset += 3;
      if (lastOffset < offset) {
        return calendar;
      }
      // ms:
      calendar.set(Calendar.MILLISECOND, Integer.parseInt(source.substring(offset, offset + 3)));
      offset += 3; // last one, move to next char
      if (lastOffset == offset) {
        return calendar;
      }
    } catch (Exception e) {
      ParseException pe = new ParseException("Improperly formatted date: " + source, offset);
      pe.initCause(e);
      throw pe;
    }
    throw new ParseException("Improperly formatted date: " + source, offset);
  }