Example usage for java.text SimpleDateFormat set2DigitYearStart

List of usage examples for java.text SimpleDateFormat set2DigitYearStart

Introduction

In this page you can find the example usage for java.text SimpleDateFormat set2DigitYearStart.

Prototype

public void set2DigitYearStart(Date startDate) 

Source Link

Document

Sets the 100-year period 2-digit years will be interpreted as being in to begin on the date the user specifies.

Usage

From source file:org.zkoss.ganttz.LeftTasksTreeRow.java

private Constraint generateConstraintForDates() {
    return new Constraint() {
        @Override/*from  w ww  .  ja  va  2  s . co  m*/
        public void validate(Component comp, Object value) throws WrongValueException {

            // Getting parameters from properties file
            int yearLimit = Integer.parseInt(properties.getProperty("yearLimit"));
            int minimumYear = Integer.parseInt(properties.getProperty("minimumYear"));

            DateTime today = new DateTime();
            DateTime maximum = today.plusYears(yearLimit);

            DateTime minimum = new DateTime(
                    new GregorianCalendar(minimumYear, MINIMUM_MONTH, MINIMUM_DAY).getTime());

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");

            // Need to call dateFormat.set2DigitYearStart to force parser not to parse date to previous century
            simpleDateFormat.set2DigitYearStart(
                    new GregorianCalendar(CALENDAR_START_YEAR, MINIMUM_MONTH, MINIMUM_DAY).getTime());

            Date date = null;

            /*
             * Need to check value type because constraint is created for textbox and datebox.
             * Textbox returns value in String. Datebox returns value in java.util.Date.
             * Also need to take last two year digits because Datebox component formats it's value.
             */

            if (value instanceof Date) {
                try {

                    // Using DateTime (Joda Time class) because java.util.Date.getYear() returns invalid value
                    DateTime correct = new DateTime(value);
                    String year = Integer.valueOf(correct.getYear()).toString().substring(2);

                    // TODO Resolve deprecated methods
                    date = simpleDateFormat
                            .parse(((Date) value).getMonth() + "/" + ((Date) value).getDate() + "/" + year);

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    date = simpleDateFormat.parse((String) value);
                } catch (ParseException ignored) {
                }
            }

            DateTime dateTimeInTextbox = new DateTime(date);

            if (dateTimeInTextbox.isAfter(maximum)) {
                throw new WrongValueException(comp,
                        _("The date you entered is invalid") + ". " + _("Please enter date not before") + " "
                                + minimumYear + " " + _("and no later than") + " " + maximum.getYear());
            }
            if (dateTimeInTextbox.isBefore(minimum)) {
                throw new WrongValueException(comp,
                        _("The date you entered is invalid") + ". " + _("Please enter date not before") + " "
                                + minimumYear + " " + _("and no later than") + " " + maximum.getYear());
            }
        }
    };
}