java.text.SimpleDateFormat#set2DigitYearStart ( )源码实例Demo

下面列出了java.text.SimpleDateFormat#set2DigitYearStart ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: j2objc   文件: SimpleDateFormatTest.java
public void test2DigitYearStartIsCloned() throws Exception {
    // Test that get2DigitYearStart returns a clone.
    SimpleDateFormat sdf = new SimpleDateFormat();
    Date originalDate = sdf.get2DigitYearStart();
    assertNotSame(sdf.get2DigitYearStart(), originalDate);
    assertEquals(sdf.get2DigitYearStart(), originalDate);
    originalDate.setTime(0);
    assertFalse(sdf.get2DigitYearStart().equals(originalDate));
    // Test that set2DigitYearStart takes a clone.
    Date newDate = new Date();
    sdf.set2DigitYearStart(newDate);
    assertNotSame(sdf.get2DigitYearStart(), newDate);
    assertEquals(sdf.get2DigitYearStart(), newDate);
    newDate.setTime(0);
    assertFalse(sdf.get2DigitYearStart().equals(newDate));
}
 
源代码2 项目: jdk1.8-source-analysis   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码3 项目: dragonwell8_jdk   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码4 项目: TencentKona-8   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码5 项目: jdk8u60   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码6 项目: astor   文件: FastDateParserTest.java
@Test
// Check that all Locales can parse the formats we use
public void testParses() throws Exception {
    for(Locale locale : Locale.getAvailableLocales()) {
        for(TimeZone tz : new TimeZone[]{NEW_YORK, GMT}) {
            Calendar cal = Calendar.getInstance(tz);
            for(int year : new int[]{2003, 1940, 1868, 1867, 0, -1940}) {
                // http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
                if (year < 1868 && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
                    continue; // Japanese imperial calendar does not support eras before 1868
                }
                cal.clear();
                if (year < 0) {
                    cal.set(-year, 1, 10);
                    cal.set(Calendar.ERA, GregorianCalendar.BC);
                } else {
                    cal.set(year, 1, 10);
                }
                Date in = cal.getTime();
                for(String format : new String[]{LONG_FORMAT, SHORT_FORMAT}) {
                    SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
                    if (format.equals(SHORT_FORMAT)) {
                        if (year < 1930) {
                            sdf.set2DigitYearStart(cal.getTime());
                        }
                    }
                    String fmt = sdf.format(in);
                    try {
                        Date out = sdf.parse(fmt);

                        assertEquals(locale.toString()+" "+year+" "+ format+ " "+tz.getID(), in, out);
                    } catch (ParseException pe) {
                        System.out.println(fmt+" "+locale.toString()+" "+year+" "+ format+ " "+tz.getID());
                        throw pe;
                    }
                }
            }
        }
    }
}
 
源代码7 项目: openjdk-jdk8u   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码8 项目: openjdk-jdk8u-backup   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码9 项目: Bytecoder   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码10 项目: astor   文件: FastDateParserTest.java
@Test
// Check that all Locales can parse the formats we use
public void testParses() throws Exception {
    for(final Locale locale : Locale.getAvailableLocales()) {
        for(final TimeZone tz : new TimeZone[]{NEW_YORK, GMT}) {
            final Calendar cal = Calendar.getInstance(tz);
            for(final int year : new int[]{2003, 1940, 1868, 1867, 0, -1940}) {
                // http://docs.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
                if (year < 1868 && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
                    continue; // Japanese imperial calendar does not support eras before 1868
                }
                cal.clear();
                if (year < 0) {
                    cal.set(-year, 1, 10);
                    cal.set(Calendar.ERA, GregorianCalendar.BC);
                } else {
                    cal.set(year, 1, 10);
                }
                final Date in = cal.getTime();
                for(final String format : new String[]{LONG_FORMAT, SHORT_FORMAT}) {
                    final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
                    if (format.equals(SHORT_FORMAT)) {
                        if (year < 1930) {
                            sdf.set2DigitYearStart(cal.getTime());
                        }
                    }
                    final String fmt = sdf.format(in);
                    try {
                        final Date out = sdf.parse(fmt);

                        assertEquals(locale.toString()+" "+year+" "+ format+ " "+tz.getID(), in, out);
                    } catch (final ParseException pe) {
                        System.out.println(fmt+" "+locale.toString()+" "+year+" "+ format+ " "+tz.getID());
                        throw pe;
                    }
                }
            }
        }
    }
}
 
源代码11 项目: jdk8u-jdk   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码12 项目: Java8CN   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码13 项目: hottub   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码14 项目: openjdk-8-source   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码15 项目: jus   文件: DateUtils.java
/**
 * Parses the date value using the given date formats.
 *
 * @param dateValue the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate During parsing, two digit years will be placed in the range
 * <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 * be <code>null</code>. When <code>null</code> is given as a parameter, year
 * <code>2000</code> will be used.
 *
 * @return the parsed date
 *
 * @throws DateParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(
        String dateValue,
        String[] dateFormats,
        Date startDate
) throws DateParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1
            && dateValue.startsWith("'")
            && dateValue.endsWith("'")
            ) {
        dateValue = dateValue.substring (1, dateValue.length() - 1);
    }

    for (String dateFormat : dateFormats) {
        SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
        dateParser.set2DigitYearStart(startDate);

        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new DateParseException("Unable to parse the date " + dateValue);
}
 
源代码16 项目: jdk8u_jdk   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
源代码17 项目: SaveVolley   文件: DateUtils.java
/**
 * Parses the date value using the given date formats.
 *
 * @param dateValue the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate During parsing, two digit years will be placed in the range
 * <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 * be <code>null</code>. When <code>null</code> is given as a parameter, year
 * <code>2000</code> will be used.
 * @return the parsed date
 * @throws DateParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, String[] dateFormats, Date startDate)
        throws DateParseException {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_PATTERNS;
    }
    if (startDate == null) {
        startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    for (String dateFormat : dateFormats) {
        SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
        dateParser.set2DigitYearStart(startDate);

        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new DateParseException("Unable to parse the date " + dateValue);
}
 
源代码18 项目: jdk8u-jdk   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}
 
private UmmalquraCalendar parseDate(String dateText, String fmt, Locale l)
        throws ParseException {
    UmmalquraCalendar uCal = new UmmalquraCalendar(l);

    SimpleDateFormat dateFormat = new SimpleDateFormat(fmt, l);
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    dateFormat.setCalendar(uCal);
    uCal.set(Calendar.YEAR, 1420);
    dateFormat.set2DigitYearStart(uCal.getTime());

    uCal.setTime(dateFormat.parse(dateText));
    return uCal;
}
 
源代码20 项目: jdk8u-dev-jdk   文件: HttpCookie.java
private long expiryDate2DeltaSeconds(String dateString) {
    Calendar cal = new GregorianCalendar(GMT);
    for (int i = 0; i < COOKIE_DATE_FORMATS.length; i++) {
        SimpleDateFormat df = new SimpleDateFormat(COOKIE_DATE_FORMATS[i],
                                                   Locale.US);
        cal.set(1970, 0, 1, 0, 0, 0);
        df.setTimeZone(GMT);
        df.setLenient(false);
        df.set2DigitYearStart(cal.getTime());
        try {
            cal.setTime(df.parse(dateString));
            if (!COOKIE_DATE_FORMATS[i].contains("yyyy")) {
                // 2-digit years following the standard set
                // out it rfc 6265
                int year = cal.get(Calendar.YEAR);
                year %= 100;
                if (year < 70) {
                    year += 2000;
                } else {
                    year += 1900;
                }
                cal.set(Calendar.YEAR, year);
            }
            return (cal.getTimeInMillis() - whenCreated) / 1000;
        } catch (Exception e) {
            // Ignore, try the next date format
        }
    }
    return 0;
}