Code example for Calendar

Methods: set, setTime, setTimeZone

0
    { 
      final Object o = getDataRow().get(timeField);
      if (o instanceof Number)
      { 
        final Number n = (Number) o;
        calendar.setTime(new Date(n.longValue()));
      } 
      else if (o instanceof Date)
      { 
        final Date d = (Date) o;
        calendar.setTime(d);
      } 
    } 
 
    if (epochTimeField != null)
    { 
      final Object o = getDataRow().get(epochTimeField);
      if (o instanceof Number)
      { 
        final Number n = (Number) o;
        calendar.setTime(new Date(n.longValue() * 1000));
      } 
    } 
    if (monthField != null)
    { 
      trySetField(calendar, Calendar.MONTH, monthField);
    } 
    if (dayOfMonthField != null)
    { 
      trySetField(calendar, Calendar.DAY_OF_MONTH, dayOfMonthField);
    } 
    if (yearField != null)
    { 
      trySetField(calendar, Calendar.YEAR, yearField);
    } 
    if (hourField != null)
    { 
      trySetField(calendar, Calendar.HOUR_OF_DAY, hourField);
    } 
    if (minuteField != null)
    { 
      trySetField(calendar, Calendar.MINUTE, minuteField);
    } 
    if (secondField != null)
    { 
      trySetField(calendar, Calendar.SECOND, secondField);
    } 
    if (milliSecondField != null)
    { 
      trySetField(calendar, Calendar.MILLISECOND, milliSecondField);
    } 
    if (dayOfWeekField != null)
    { 
      trySetField(calendar, Calendar.DAY_OF_WEEK, dayOfWeekField);
    } 
    if (dayOfYearField != null)
    { 
      trySetField(calendar, Calendar.DAY_OF_YEAR, dayOfYearField);
    } 
    if (dayOfWeekInMonthField != null)
    { 
      trySetField(calendar, Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonthField);
    } 
    if (weekOfMonthField != null)
    { 
      trySetField(calendar, Calendar.WEEK_OF_MONTH, weekOfMonthField);
    } 
    if (weekOfYearField != null)
    { 
      trySetField(calendar, Calendar.WEEK_OF_YEAR, weekOfYearField);
    } 
    if (timeZoneField != null)
    { 
      final Object o = getDataRow().get(getTimeZoneField());
      if (o instanceof String)
      { 
        calendar.setTimeZone(TimeZone.getTimeZone((String) o));
      } 
      else if (o instanceof TimeZone)
      { 
        calendar.setTimeZone((TimeZone) o);
      } 
    } 
  } 
 
  /** 
   * A helper method tha tries to update a field from a column in the data-row. The calendar is only updated, if the 
   * field contains a Number. 
   * 
   * @param calendar the calendar that should be updated 
   * @param field    the field as specified in the Calendar class 
   * @param column   the data-row column from where to read the number 
   */ 
  private void trySetField(final Calendar calendar, final int field, final String column)
  { 
    if (column == null)
    { 
      return; 
    } 
    final Object o = getDataRow().get(column);
    if (o instanceof Number == false)
    { 
      return; 
    } 
    final Number n = (Number) o;
    calendar.set(field, n.intValue());
  } 
}