本文整理匯總了Java中java.text.SimpleDateFormat.set2DigitYearStart方法的典型用法代碼示例。如果您正苦於以下問題:Java SimpleDateFormat.set2DigitYearStart方法的具體用法?Java SimpleDateFormat.set2DigitYearStart怎麽用?Java SimpleDateFormat.set2DigitYearStart使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat.set2DigitYearStart方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parseDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
* 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 or null if input could not be parsed
*/
public static Date parseDate(
final String dateValue,
final String[] dateFormats,
final Date startDate) {
Args.notNull(dateValue, "Date value");
final String[] localDateFormats = dateFormats != null ? dateFormats : DEFAULT_PATTERNS;
final Date localStartDate = startDate != null ? startDate : DEFAULT_TWO_DIGIT_YEAR_START;
String v = dateValue;
// trim single quotes around date if present
// see issue #5279
if (v.length() > 1 && v.startsWith("'") && v.endsWith("'")) {
v = v.substring (1, v.length() - 1);
}
for (final String dateFormat : localDateFormats) {
final SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
dateParser.set2DigitYearStart(localStartDate);
final ParsePosition pos = new ParsePosition(0);
final Date result = dateParser.parse(v, pos);
if (pos.getIndex() != 0) {
return result;
}
}
return null;
}
示例2: parseDate
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
/**
* 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);
}
示例3: expiryDate2DeltaSeconds
import java.text.SimpleDateFormat; //導入方法依賴的package包/類
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;
}