Example usage for com.google.gwt.i18n.shared.impl DateRecord setAmbiguousYear

List of usage examples for com.google.gwt.i18n.shared.impl DateRecord setAmbiguousYear

Introduction

In this page you can find the example usage for com.google.gwt.i18n.shared.impl DateRecord setAmbiguousYear.

Prototype

public void setAmbiguousYear(boolean ambiguousYear) 

Source Link

Document

Set ambiguous year field.

Usage

From source file:com.rnb.plategka.shared.DateTimeFormat.java

License:Apache License

/**
 * Method subParseYear parse year field. Year field is special because 1,
 * two digit year need to be resolved. 2, we allow year to take a sign. 3,
 * year field participate in abut processing. In my testing, negative year
 * does not seem working due to JDK (or GWT implementation) limitation. It
 * is not a big deal so we don't worry about it. But keep the logic here so
 * that we might want to replace DateRecord with our a calendar class.
 * /* www  .ja v  a 2  s  .  co  m*/
 * @param text
 *            the time text to be parsed
 * @param pos
 *            parse position
 * @param start
 *            where this field starts
 * @param value
 *            integer value of year
 * @param part
 *            the pattern part for this field
 * @param cal
 *            DateRecord object that will hold parsed value
 * 
 * @return <code>true</code> if successful
 */
private boolean subParseYear(String text, int[] pos, int start, int value, PatternPart part, DateRecord cal) {
    char ch = ' ';
    if (value < 0) {
        if (pos[0] >= text.length()) {
            return false;
        }
        ch = text.charAt(pos[0]);
        // Check if it is a sign.
        if (ch != '+' && ch != '-') {
            return false;
        }
        ++(pos[0]);
        value = parseInt(text, pos);
        if (value < 0) {
            return false;
        }
        if (ch == '-') {
            value = -value;
        }
    }

    // no sign, only 2 digit was actually parsed, pattern say it has 2 digit.
    if (ch == ' ' && (pos[0] - start) == 2 && part.count == 2) {
        // Assume for example that the defaultCenturyStart is 6/18/1903.
        // This means that two-digit years will be forced into the range
        // 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02
        // correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
        // to 1904, 1905, etc. If the year is 03, then it is 2003 if the
        // other fields specify a date before 6/18, or 1903 if they specify a
        // date afterwards. As a result, 03 is an ambiguous year. All other
        // two-digit years are unambiguous.
        Date date = new Date();
        @SuppressWarnings("deprecation")
        int defaultCenturyStartYear = date.getYear() + 1900 - 80;
        int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
        cal.setAmbiguousYear(value == ambiguousTwoDigitYear);
        value += (defaultCenturyStartYear / 100) * 100 + (value < ambiguousTwoDigitYear ? 100 : 0);
    }
    cal.setYear(value);
    return true;
}