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

The following examples show how to use java.util.Calendar#isSet() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: AndroidHttpCapture   File: DatatypeConverter.java    License: 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 2
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
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 4
Source Project: JDKSourceCode1.8   File: DatatypeConverterImpl.java    License: 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 5
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
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 Project: ion-java   File: Timestamp.java    License: 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 8
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
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
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 Project: Java8CN   File: DatatypeConverterImpl.java    License: 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 12
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 13
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
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
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 Project: lucene-solr   File: DateRangePrefixTree.java    License: 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 17
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 Project: Aria   File: FTPFile.java    License: 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 19
Source Project: ion-java   File: Timestamp.java    License: 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 20
Source Project: valifi   File: ValiFieldDate.java    License: 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
}