Beispiel #1
0
 /* The result is the datetime value consisting of the parts given by year, month, day, hour, minute and second.
  */
 public static SchemaDateTime datetimeFromParts(
     SchemaTypeNumber year,
     SchemaTypeNumber month,
     SchemaTypeNumber day,
     SchemaTypeNumber hour /* optional */,
     SchemaTypeNumber minute /* optional */,
     SchemaTypeNumber second /* optional */,
     SchemaTypeNumber millisecond /* optional */,
     SchemaTypeNumber timezone /* optional */) {
   SchemaDateTime result =
       new SchemaDateTime(
           year.intValue(),
           month.intValue(),
           day.intValue(),
           hour.intValue(),
           minute.intValue(),
           second.intValue(),
           0);
   result.setMillisecond(millisecond.intValue());
   long tv = result.getTimeValue();
   result.setTimeFromTimeValue(tv);
   result.setDateFromTimeValue(tv);
   if (timezone.intValue() >= -1440 && timezone.intValue() <= 1440)
     result.setTimezone(SchemaCalendarBase.TZ_OFFSET, timezone.intValue());
   return result;
 }
Beispiel #2
0
  /* Performs a addition of dates, times and durations.
   */
  public static SchemaDateTime datetimeAdd(SchemaDateTime datetime, SchemaDuration duration) {
    int months = duration.getYearMonthValue();
    SchemaDateTime result = new SchemaDateTime(datetime);
    result.setMonth(datetime.getMonth() + months);
    int yearsOverflow;
    if (result.getMonth() >= 1) yearsOverflow = (result.getMonth() - 1) / 12;
    else yearsOverflow = (result.getMonth() - 12) / 12;
    result.setMonth(result.getMonth() - yearsOverflow * 12);
    if (yearsOverflow > 0 && result.getYear() < 0 && yearsOverflow >= -result.getYear())
      result.setYear(result.getYear() + yearsOverflow + 1);
    else if (yearsOverflow < 0 && result.getYear() > 0 && -yearsOverflow >= result.getYear())
      result.setYear(result.getYear() + yearsOverflow - 1);
    else result.setYear(result.getYear() + yearsOverflow);

    boolean leapYear =
        (result.getYear() % 4 == 0)
            && ((result.getYear() % 100 != 0) || (result.getYear() % 400 == 0));
    int daysInMonth =
        result.getMonth() == 2
            ? leapYear ? 29 : 28
            : (30 + ((result.getMonth() & 1) ^ ((result.getMonth() >> 3) & 1)));
    if (result.getDay() > daysInMonth) result.setDay(daysInMonth);

    long tv =
        result.getTimeValue() + duration.getDayTimeValue() + result.getTimezoneOffset() * 60000;
    result.setTimeFromTimeValue(tv);
    result.setDateFromTimeValue(tv);
    return result;
  }