public static XMLGregorianCalendar create(
      int year, int month, int day, int hr, int min, int sec) {
    GregorianCalendar cal = new GregorianCalendar(Locale.US);

    cal.clear();
    cal.setLenient(false);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.HOUR_OF_DAY, hr);
    cal.set(Calendar.MINUTE, min);
    cal.set(Calendar.SECOND, sec);

    return DATATYPE_FACTORY.newXMLGregorianCalendar(cal);
  }
 /**
  * Get a Java Calendar object representing the value of this DateTime. This will respect the
  * timezone if there is one, or be in GMT otherwise.
  *
  * @return a Java GregorianCalendar object representing the value of this xs:dateTime value.
  */
 public GregorianCalendar getCalendar() {
   int tz = (hasTimezone() ? getTimezoneInMinutes() * 60000 : 0);
   TimeZone zone = new SimpleTimeZone(tz, "LLL");
   GregorianCalendar calendar = new GregorianCalendar(zone);
   calendar.setGregorianChange(new Date(Long.MIN_VALUE));
   calendar.setLenient(false);
   int yr = year;
   if (year <= 0) {
     yr = 1 - year;
     calendar.set(Calendar.ERA, GregorianCalendar.BC);
   }
   calendar.set(yr, month - 1, day, hour, minute, second);
   calendar.set(Calendar.MILLISECOND, microsecond / 1000); // loses precision unavoidably
   calendar.set(Calendar.ZONE_OFFSET, tz);
   //        Note JDK 1.5 dependency. JDK 1.4 limited the range to +|- 12 hours, so the code was:
   //        if (tz >= -12*60*60*1000 && tz <= +12*60*60*1000) {
   //            calendar.set(Calendar.ZONE_OFFSET, tz);
   //        }
   calendar.set(Calendar.DST_OFFSET, 0);
   return calendar;
 }
Beispiel #3
0
  public void test(TestHarness harness) {
    GregorianCalendar cal;
    cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US);

    // Month with 6 weeks
    cal.set(2006, Calendar.JULY, 30);
    cal.setLenient(false);

    harness.check(cal.getMaximum(Calendar.WEEK_OF_MONTH), 6);

    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 1);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 1);

    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 6);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 31);

    // Month with 4 weeks
    cal.set(1998, Calendar.FEBRUARY, 14);
    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 1);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 1);

    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 4);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 28);
    harness.check(cal.get(Calendar.MONTH), Calendar.FEBRUARY);

    // Month with 5 weeks
    cal.set(1993, Calendar.FEBRUARY, 14);
    cal.clear(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.WEEK_OF_MONTH, 5);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    harness.check(cal.get(Calendar.DAY_OF_MONTH), 28);
  }