Java Code Examples for java.util.Calendar#isSet()

The following examples show how to use java.util.Calendar#isSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 2
Source File: DatatypeConverterImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 3
Source File: DateRangePrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Calendar utility method:
 * Gets the Calendar field code of the last field that is set prior to an unset field. It only
 * examines fields relevant to the prefix tree. If no fields are set, it returns -1. */
public int getCalPrecisionField(Calendar cal) {
  int lastField = -1;
  for (int level = YEAR_LEVEL; level < FIELD_BY_LEVEL.length; level++) {
    int field = FIELD_BY_LEVEL[level];
    if (!cal.isSet(field))
      break;
    lastField = field;
  }
  return lastField;
}
 
Example 4
Source File: DatatypeConverterImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 5
Source File: DatatypeConverterImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 6
Source File: DatatypeConverterImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 7
Source File: DatatypeConverterImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 8
Source File: DatatypeConverterImpl.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 9
Source File: DatatypeConverterImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 10
Source File: DatatypeConverterImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 11
Source File: DatatypeConverter.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND),buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if(n!=0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3)
                ms = '0' + ms; // left 0 paddings.

            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 12
Source File: Timestamp.java    From ion-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new Timestamp from a {@link Calendar}, preserving the
 * {@link Calendar}'s precision and local offset from UTC.
 * <p>
 * The most precise calendar field of {@code cal} will be used to determine
 * the precision of the resulting Timestamp.
 *
 * For example, the calendar field will have a Timestamp precision accordingly:
 * <ul>
 *   <li>{@link Calendar#YEAR} - year precision, unknown local offset</li>
 *   <li>{@link Calendar#MONTH} - month precision, unknown local offset</li>
 *   <li>{@link Calendar#DAY_OF_MONTH} - day precision, unknown local offset</li>
 *   <li>{@link Calendar#HOUR_OF_DAY} or {@link Calendar#MINUTE} - minute precision</li>
 *   <li>{@link Calendar#SECOND} - second precision</li>
 *   <li>{@link Calendar#MILLISECOND} - fractional second precision</li>
 * </ul>
 *
 * @throws IllegalArgumentException
 *          if {@code cal} has no appropriate calendar fields set.
 *
 * @deprecated Use {@link #forCalendar(Calendar)} instead.
 */
@Deprecated
public Timestamp(Calendar cal)
{
    Precision precision;

    if (cal.isSet(Calendar.MILLISECOND) || cal.isSet(Calendar.SECOND)) {
        precision = Precision.SECOND;
    }
    else if (cal.isSet(Calendar.HOUR_OF_DAY) || cal.isSet(Calendar.MINUTE)) {
        precision = Precision.MINUTE;
    }
    else if (cal.isSet(Calendar.DAY_OF_MONTH)) {
        precision = Precision.DAY;
    }
    else if (cal.isSet(Calendar.MONTH)) {
        precision = Precision.MONTH;
    }
    else if (cal.isSet(Calendar.YEAR)) {
        precision = Precision.YEAR;
    }
    else {
        throw new IllegalArgumentException("Calendar has no fields set");
    }

    set_fields_from_calendar(cal, precision, true);
}
 
Example 13
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 14
Source File: DatatypeConverterImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 15
Source File: DatatypeConverterImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 16
Source File: DatatypeConverterImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 17
Source File: DatatypeConverterImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void formatSeconds(Calendar cal, StringBuilder buf) {
    formatTwoDigits(cal.get(Calendar.SECOND), buf);
    if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds
        int n = cal.get(Calendar.MILLISECOND);
        if (n != 0) {
            String ms = Integer.toString(n);
            while (ms.length() < 3) {
                ms = '0' + ms; // left 0 paddings.
            }
            buf.append('.');
            buf.append(ms);
        }
    }
}
 
Example 18
Source File: Timestamp.java    From ion-java with Apache License 2.0 4 votes vote down vote up
/**
 * Copies data from a {@link Calendar} into this timestamp.
 * Must only be called during construction due to timestamp immutabliity.
 *
 * @param cal must have at least one field set.
 *
 * @throws IllegalArgumentException if the calendar has no fields set.
 */
private void set_fields_from_calendar(Calendar cal,
                                      Precision precision,
                                      boolean setLocalOffset)
{
    _precision = precision;
    _offset = UNKNOWN_OFFSET;
    boolean dayPrecision = false;
    boolean calendarHasMilliseconds = cal.isSet(Calendar.MILLISECOND);

    switch (this._precision) {
        case FRACTION:
        case SECOND:
            this._second = checkAndCastSecond(cal.get(Calendar.SECOND));
            if (calendarHasMilliseconds) {
                BigDecimal millis = BigDecimal.valueOf(cal.get(Calendar.MILLISECOND));
                this._fraction = millis.movePointLeft(3); // convert to fraction
                checkFraction(precision, this._fraction);
            }
        case MINUTE:
        {
            this._hour   = checkAndCastHour(cal.get(Calendar.HOUR_OF_DAY));
            this._minute = checkAndCastMinute(cal.get(Calendar.MINUTE));

            // If this test is made before calling get(), it will return
            // false even when Calendar.setTimeZone() was called.
            if (setLocalOffset && cal.isSet(Calendar.ZONE_OFFSET))
            {
                int offset = cal.get(Calendar.ZONE_OFFSET);
                if (cal.isSet(Calendar.DST_OFFSET)) {
                    offset += cal.get(Calendar.DST_OFFSET);
                }

                // convert ms to minutes
                _offset = offset / (1000*60);
            }
        }
        case DAY:
            dayPrecision = true;
        case MONTH:
            // Calendar months are 0 based, Timestamp months are 1 based
            this._month  = checkAndCastMonth((cal.get(Calendar.MONTH) + 1));
        case YEAR:
            int year;
            if(cal.get(Calendar.ERA) == GregorianCalendar.AD) {
                year = cal.get(Calendar.YEAR);
            }
            else {
                year = -cal.get(Calendar.YEAR);
            }

            this._year = checkAndCastYear(year);
    }

    if (dayPrecision)
    {
        this._day = checkAndCastDay(cal.get(Calendar.DAY_OF_MONTH), _year, _month);
    }

    if (_offset != UNKNOWN_OFFSET) {
        // Transform our members from local time to Zulu
        this.apply_offset(_offset);
    }
}
 
Example 19
Source File: FTPFile.java    From Aria with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a string representation of the FTPFile information.
 * This currently mimics the Unix listing format.
 * This method allows the Calendar time zone to be overridden.
 * <p>
 * Note: if the instance is not valid {@link #isValid()}, no useful
 * information can be returned. In this case, use {@link #getRawListing()}
 * instead.
 *
 * @param timezone the timezone to use for displaying the time stamp
 * If {@code null}, then use the Calendar entry timezone
 * @return A string representation of the FTPFile information.
 * @since 3.4
 */
public String toFormattedString(final String timezone) {

  if (!isValid()) {
    return "[Invalid: could not parse file entry]";
  }
  StringBuilder sb = new StringBuilder();
  Formatter fmt = new Formatter(sb);
  sb.append(formatType());
  sb.append(permissionToString(USER_ACCESS));
  sb.append(permissionToString(GROUP_ACCESS));
  sb.append(permissionToString(WORLD_ACCESS));
  fmt.format(" %4d", Integer.valueOf(getHardLinkCount()));
  fmt.format(" %-8s %-8s", getUser(), getGroup());
  fmt.format(" %8d", Long.valueOf(getSize()));
  Calendar timestamp = getTimestamp();
  if (timestamp != null) {
    if (timezone != null) {
      TimeZone newZone = TimeZone.getTimeZone(timezone);
      if (!newZone.equals(timestamp.getTimeZone())) {
        Date original = timestamp.getTime();
        Calendar newStamp = Calendar.getInstance(newZone);
        newStamp.setTime(original);
        timestamp = newStamp;
      }
    }
    fmt.format(" %1$tY-%1$tm-%1$td", timestamp);
    // Only display time units if they are present
    if (timestamp.isSet(Calendar.HOUR_OF_DAY)) {
      fmt.format(" %1$tH", timestamp);
      if (timestamp.isSet(Calendar.MINUTE)) {
        fmt.format(":%1$tM", timestamp);
        if (timestamp.isSet(Calendar.SECOND)) {
          fmt.format(":%1$tS", timestamp);
          if (timestamp.isSet(Calendar.MILLISECOND)) {
            fmt.format(".%1$tL", timestamp);
          }
        }
      }
      fmt.format(" %1$tZ", timestamp);
    }
  }
  sb.append(' ');
  sb.append(getName());
  fmt.close();
  return sb.toString();
}
 
Example 20
Source File: ValiFieldDate.java    From valifi with Apache License 2.0 2 votes vote down vote up
/**
 * Checking for specific type if value is empty.
 * Used for checking if empty is allowed.
 *
 * @param actualValue value when checking
 * @return true when value is empty, false when values is not empty (e.g for String, use isEmpty())
 * @see #mCallback
 */
@Override
protected boolean whenThisFieldIsEmpty(@NonNull Calendar actualValue) {
	return !actualValue.isSet(Calendar.YEAR);    // TODO maybe check other values
}