下面列出了java.text.SimpleDateFormat#set2DigitYearStart ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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));
}
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 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 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 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;
}
@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;
}
}
}
}
}
}
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 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 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;
}
@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;
}
}
}
}
}
}
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 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 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 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;
}
/**
* 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);
}
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;
}
/**
* 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);
}
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;
}
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;
}