Java Code Examples for java.util.GregorianCalendar#setLenient()

The following examples show how to use java.util.GregorianCalendar#setLenient() . 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: AbstractProRation.java    From OpenRate with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor - Create the class
 */
public AbstractProRation()
{
  super();

  // get the calendar object for working with the dates
  workingCalendar = new GregorianCalendar();
  workingCalendar.setLenient(false);

  // get the calendar helper object
  helperCalendar = new GregorianCalendar();

  SimpleDateFormat sdfIn = new SimpleDateFormat("dd/MM/yyyy");
  String firstJanDate = "01/01/2000";

  try
  {
    FirstJan2000 = sdfIn.parse(firstJanDate);
  }
  catch (ParseException ex)
  {
    message = "Error getting date in <" + getSymbolicName() + ">";
    getPipeLog().fatal(message);
  }
}
 
Example 2
Source File: Clock.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
/**
 * An instant in time specified by number year, number month, number day
 * @param year year integer
 * @param month month integer
 * @param day day integer
 * @return  Calendar instant
 * @suppressdoc
 */
@SimpleFunction(description = "Returns an instant in time specified by year, month, date in "
    + "UTC.\nValid values for the month field are 1-12 and 1-31 for the day field.")
public Calendar MakeDate(int year, int month, int day) {
  int jMonth = month - 1;
  try {
    GregorianCalendar cal = new GregorianCalendar(year, jMonth, day);
    cal.setLenient(false);

    // A non-lenient GregorianCalendar throws an exception upon 
    // calculating its time or calendar field values if any out-of-range field value has been set.
    cal.getTime();
  } catch (IllegalArgumentException e) {
    form.dispatchErrorOccurredEvent(this, "MakeDate", ErrorMessages.ERROR_ILLEGAL_DATE);
  }
  
  Calendar instant = Dates.DateInstant(year, month, day);
  return instant;
}
 
Example 3
Source File: ZoneOffsets.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    for (int l = 0; l < locales.length; l++) {
        Locale loc = locales[l];
        for (int i = 0; i < offsets.length; i++) {
            test(loc, offsets[i][0], offsets[i][1]);
        }
    }

    // The test case in the bug report.
    GregorianCalendar cal = new GregorianCalendar();
    cal.setLenient(false);
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.clear();
    cal.set(ZONE_OFFSET, 0);
    cal.set(DST_OFFSET, 0);
    cal.set(ERA, AD);
    cal.set(2004, FEBRUARY, 3, 0, 0, 0);
    cal.set(MILLISECOND, 0);
    // The following line should not throw an IllegalArgumentException.
    cal.getTime();
}
 
Example 4
Source File: CalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void dowTest(boolean lenient) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(1997, AUGUST, 12); // Wednesday
    cal.getTime(); // Force update
    cal.setLenient(lenient);
    cal.set(1996, DECEMBER, 1); // Set the date to be December 1, 1996
    int dow = cal.get(DAY_OF_WEEK);
    int min = cal.getMinimum(DAY_OF_WEEK);
    int max = cal.getMaximum(DAY_OF_WEEK);
    if (dow < min || dow > max) {
        errln("FAIL: Day of week " + dow + " out of range");
    }
    if (dow != SUNDAY) {
        errln("FAIL2: Day of week should be SUNDAY; is " + dow + ": " + cal.getTime());
    }
    if (min != SUNDAY || max != SATURDAY) {
        errln("FAIL: Min/max bad");
    }
}
 
Example 5
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void dowTest(boolean lenient) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(1997, AUGUST, 12); // Wednesday
    // cal.getTime(); // Force update
    cal.setLenient(lenient);
    cal.set(1996, DECEMBER, 1); // Set the date to be December 1, 1996
    int dow = cal.get(DAY_OF_WEEK);
    int min = cal.getMinimum(DAY_OF_WEEK);
    int max = cal.getMaximum(DAY_OF_WEEK);
    logln(cal.getTime().toString());
    if (min != SUNDAY || max != SATURDAY) {
        errln("FAIL: Min/max bad");
    }
    if (dow < min || dow > max) {
        errln("FAIL: Day of week " + dow + " out of range");
    }
    if (dow != SUNDAY) {
        errln("FAIL: Day of week should be SUNDAY Got " + dow);
    }
}
 
Example 6
Source File: Bug6178071.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    GregorianCalendar cal = new GregorianCalendar(2004, JANUARY, 1);
    cal.set(HOUR, 1);
    if (cal.get(HOUR_OF_DAY) != 1 ||
        cal.get(HOUR) != 1 || cal.get(AM_PM) != AM) {
        throw new RuntimeException("Unexpected hour of day: " + cal.getTime());
    }

    // Test case for 6440854
    GregorianCalendar gc = new GregorianCalendar(2006,5,16);
    gc.setLenient(false);
    gc.set(HOUR_OF_DAY, 10);
    // The following line shouldn't throw an IllegalArgumentException.
    gc.get(YEAR);
}
 
Example 7
Source File: DateConverter.java    From PdfBox-Android with Apache License 2.0 5 votes vote down vote up
static GregorianCalendar newGreg()
{
    GregorianCalendar retCal = new GregorianCalendar(Locale.ENGLISH);
    retCal.setTimeZone(new SimpleTimeZone(0, "UTC"));
    retCal.setLenient(false);
    retCal.set(Calendar.MILLISECOND, 0);
    return retCal;
}
 
Example 8
Source File: DateConverter.java    From sambox with Apache License 2.0 5 votes vote down vote up
static GregorianCalendar newGreg()
{
    GregorianCalendar retCal = new GregorianCalendar(Locale.ENGLISH);
    retCal.setTimeZone(new SimpleTimeZone(0, "UTC"));
    retCal.setLenient(false);
    retCal.set(Calendar.MILLISECOND, 0);
    return retCal;
}
 
Example 9
Source File: Bug6178071.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    GregorianCalendar cal = new GregorianCalendar(2004, JANUARY, 1);
    cal.set(HOUR, 1);
    if (cal.get(HOUR_OF_DAY) != 1 ||
        cal.get(HOUR) != 1 || cal.get(AM_PM) != AM) {
        throw new RuntimeException("Unexpected hour of day: " + cal.getTime());
    }

    // Test case for 6440854
    GregorianCalendar gc = new GregorianCalendar(2006,5,16);
    gc.setLenient(false);
    gc.set(HOUR_OF_DAY, 10);
    // The following line shouldn't throw an IllegalArgumentException.
    gc.get(YEAR);
}
 
Example 10
Source File: DateConverter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
static GregorianCalendar newGreg()
{
    GregorianCalendar retCal = new GregorianCalendar(Locale.ENGLISH);
    retCal.setTimeZone(new SimpleTimeZone(0, "UTC"));
    retCal.setLenient(false);
    retCal.set(Calendar.MILLISECOND, 0);
    return retCal;
}
 
Example 11
Source File: DatePicker.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
@SimpleFunction(description = "Allows the user to set the date to be displayed when the date picker opens.\n" +
  "Valid values for the month field are 1-12 and 1-31 for the day field.\n")
public void SetDateToDisplay(int year, int month, int day) {
  int jMonth = month - 1;
  try {
    GregorianCalendar cal = new GregorianCalendar(year, jMonth, day);
    cal.setLenient(false);
    cal.getTime();
  } catch (java.lang.IllegalArgumentException e) {
    form.dispatchErrorOccurredEvent(this, "SetDateToDisplay", ErrorMessages.ERROR_ILLEGAL_DATE);
  }
  date.updateDate(year, jMonth, day);
  instant = Dates.DateInstant(year, month, day);
  customDate = true;
}
 
Example 12
Source File: bug4401223.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void Test4401223b() {
    int status = 0;
    String s = null;

    try {
        @SuppressWarnings("deprecation")
        Date date = new Date(2000 - 1900, DECEMBER, 31);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.setLenient(false);
        gc.set(YEAR, 2001);

        if (gc.get(YEAR) != 2001
                || gc.get(MONTH) != DECEMBER
                || gc.get(DATE) != 31
                || gc.get(DAY_OF_YEAR) != 365) {
            status++;
            s = "Wrong Date : 12/31/00 & set(YEAR,2001) ---> " + gc.getTime().toString();
        } else {
            s = "12/31/00 & set(YEAR,2001) = " + gc.getTime().toString();
        }
    } catch (Exception ex) {
        status++;
        s = "Exception occurred for 12/31/00 & set(YEAR,2001) : " + ex;
    }
    if (status > 0) {
        errln(s);
    } else {
        logln(s);
    }
}
 
Example 13
Source File: Bug6178071.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    GregorianCalendar cal = new GregorianCalendar(2004, JANUARY, 1);
    cal.set(HOUR, 1);
    if (cal.get(HOUR_OF_DAY) != 1 ||
        cal.get(HOUR) != 1 || cal.get(AM_PM) != AM) {
        throw new RuntimeException("Unexpected hour of day: " + cal.getTime());
    }

    // Test case for 6440854
    GregorianCalendar gc = new GregorianCalendar(2006,5,16);
    gc.setLenient(false);
    gc.set(HOUR_OF_DAY, 10);
    // The following line shouldn't throw an IllegalArgumentException.
    gc.get(YEAR);
}
 
Example 14
Source File: Bug6178071.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    GregorianCalendar cal = new GregorianCalendar(2004, JANUARY, 1);
    cal.set(HOUR, 1);
    if (cal.get(HOUR_OF_DAY) != 1 ||
        cal.get(HOUR) != 1 || cal.get(AM_PM) != AM) {
        throw new RuntimeException("Unexpected hour of day: " + cal.getTime());
    }

    // Test case for 6440854
    GregorianCalendar gc = new GregorianCalendar(2006,5,16);
    gc.setLenient(false);
    gc.set(HOUR_OF_DAY, 10);
    // The following line shouldn't throw an IllegalArgumentException.
    gc.get(YEAR);
}
 
Example 15
Source File: Bug6178071.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    GregorianCalendar cal = new GregorianCalendar(2004, JANUARY, 1);
    cal.set(HOUR, 1);
    if (cal.get(HOUR_OF_DAY) != 1 ||
        cal.get(HOUR) != 1 || cal.get(AM_PM) != AM) {
        throw new RuntimeException("Unexpected hour of day: " + cal.getTime());
    }

    // Test case for 6440854
    GregorianCalendar gc = new GregorianCalendar(2006,5,16);
    gc.setLenient(false);
    gc.set(HOUR_OF_DAY, 10);
    // The following line shouldn't throw an IllegalArgumentException.
    gc.get(YEAR);
}
 
Example 16
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * This is a bug in the validation code of GregorianCalendar.  As reported,
 * the bug seems worse than it really is, due to a bug in the way the bug
 * report test was written.  In reality the bug is restricted to the DAY_OF_YEAR
 * field. - liu 6/29/98
 */
public void Test4147269() {
    final String[] fieldName = {
        "ERA",
        "YEAR",
        "MONTH",
        "WEEK_OF_YEAR",
        "WEEK_OF_MONTH",
        "DAY_OF_MONTH",
        "DAY_OF_YEAR",
        "DAY_OF_WEEK",
        "DAY_OF_WEEK_IN_MONTH",
        "AM_PM",
        "HOUR",
        "HOUR_OF_DAY",
        "MINUTE",
        "SECOND",
        "MILLISECOND",
        "ZONE_OFFSET",
        "DST_OFFSET"};
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setLenient(false);
    @SuppressWarnings("deprecation")
    Date date = new Date(1996 - 1900, JANUARY, 3); // Arbitrary date
    for (int field = 0; field < FIELD_COUNT; field++) {
        calendar.setTime(date);
        // Note: In the bug report, getActualMaximum() was called instead
        // of getMaximum() -- this was an error.  The validation code doesn't
        // use getActualMaximum(), since that's too costly.
        int max = calendar.getMaximum(field);
        int value = max + 1;
        calendar.set(field, value);
        try {
            calendar.getTime(); // Force time computation
            // We expect an exception to be thrown. If we fall through
            // to the next line, then we have a bug.
            errln("Test failed with field " + fieldName[field]
                    + ", date before: " + date
                    + ", date after: " + calendar.getTime()
                    + ", value: " + value + " (max = " + max + ")");
        } catch (IllegalArgumentException e) {
        }
    }
}
 
Example 17
Source File: DatePicker.java    From spacewalk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new date picker that extracts fields prefixed with
 * <code>name0 + "_"</code> and works with the given locale.
 *
 * @param name0 the prefix for the subfields for the date picker
 * @param locale0 the locale to use
 * @param yearRangeDirection0 direction of the year range to use.
 * YEAR_RANGE_POSATIVE means the year selection will go from now
 * until YEAR_RANGE_SIZE in the future (2005-2010). YEAR_RANGE_NEGATIVE
 * will include a range from now until YEAR_RANGE_SIZE in the
 * past (2000 - 2005)
 */
public DatePicker(String name0, Locale locale0, int yearRangeDirection0) {
    name = name0;
    locale = locale0;
    cal = new GregorianCalendar(locale0);
    cal.setLenient(false);
    currentYear = cal.get(Calendar.YEAR);
    analyzeDateFormat();
    yearRangeDirection = yearRangeDirection0;
    disableTime = true;
    disableDate = false;
}
 
Example 18
Source File: DatePicker.java    From uyuni with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new date picker that extracts fields prefixed with
 * <code>name0 + "_"</code> and works with the given locale.
 *
 * @param name0 the prefix for the subfields for the date picker
 * @param locale0 the locale to use
 * @param yearRangeDirection0 direction of the year range to use.
 * YEAR_RANGE_POSATIVE means the year selection will go from now
 * until YEAR_RANGE_SIZE in the future (2005-2010). YEAR_RANGE_NEGATIVE
 * will include a range from now until YEAR_RANGE_SIZE in the
 * past (2000 - 2005)
 */
public DatePicker(String name0, Locale locale0, int yearRangeDirection0) {
    name = name0;
    locale = locale0;
    cal = new GregorianCalendar(locale0);
    cal.setLenient(false);
    currentYear = cal.get(Calendar.YEAR);
    analyzeDateFormat();
    yearRangeDirection = yearRangeDirection0;
    disableTime = true;
    disableDate = false;
}
 
Example 19
Source File: DatePicker.java    From uyuni with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new date picker that extracts fields prefixed with
 * <code>name0 + "_"</code> and works with the given time zone
 * and locale.
 *
 * @param name0 the prefix for the subfields for the date picker
 * @param tz the timezone in which values are to be interpreted
 * @param locale0 the locale to use
 * @param yearRangeDirection0 direction of the year range to use.
 * YEAR_RANGE_POSATIVE means the year selection will go from now
 * until YEAR_RANGE_SIZE in the future (2005-2010). YEAR_RANGE_NEGATIVE
 * will include a range from now until YEAR_RANGE_SIZE in the
 * past (2000 - 2005)
 */
public DatePicker(String name0, TimeZone tz, Locale locale0, int yearRangeDirection0) {
    name = name0;
    locale = locale0;
    cal = new GregorianCalendar(tz, locale0);
    cal.setLenient(false);
    currentYear = cal.get(Calendar.YEAR);
    analyzeDateFormat();
    yearRangeDirection = yearRangeDirection0;
    disableTime = false;
    disableDate = false;
}
 
Example 20
Source File: DatePicker.java    From spacewalk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new date picker that extracts fields prefixed with
 * <code>name0 + "_"</code> and works with the given time zone
 * and locale.
 *
 * @param name0 the prefix for the subfields for the date picker
 * @param tz the timezone in which values are to be interpreted
 * @param locale0 the locale to use
 * @param yearRangeDirection0 direction of the year range to use.
 * YEAR_RANGE_POSATIVE means the year selection will go from now
 * until YEAR_RANGE_SIZE in the future (2005-2010). YEAR_RANGE_NEGATIVE
 * will include a range from now until YEAR_RANGE_SIZE in the
 * past (2000 - 2005)
 */
public DatePicker(String name0, TimeZone tz, Locale locale0, int yearRangeDirection0) {
    name = name0;
    locale = locale0;
    cal = new GregorianCalendar(tz, locale0);
    cal.setLenient(false);
    currentYear = cal.get(Calendar.YEAR);
    analyzeDateFormat();
    yearRangeDirection = yearRangeDirection0;
    disableTime = false;
    disableDate = false;
}