Example usage for com.google.gwt.i18n.client.impl DateRecord setYear

List of usage examples for com.google.gwt.i18n.client.impl DateRecord setYear

Introduction

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

Prototype

@Override
public void setYear(int value) 

Source Link

Document

Set year field.

Usage

From source file:com.calclab.emite.core.client.xmpp.datetime.gwt.DateTimeFormat.java

License:Open Source 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.
 * /*from   ww  w  . j a  v  a2s . c om*/
 * @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();
        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;
}