📄 simpledateformat.java
字号:
count = 0;
interQuoteCount = 0;
}
else
{
// pattern uses text following from 1st single quote.
if (ch != text.charAt(start)) {
// Check for cases like: 'at' in pattern vs "xt"
// in time text, where 'a' doesn't match with 'x'.
// If fail to match, return null.
pos.index = oldStart; // left unchanged
return null;
}
++count;
++start;
}
}
else // !inQuote
{
if (ch == '\'')
{
inQuote = true;
if (count > 0) // handle cases like: e'at'
{
start=subParse(text, start, prevCh, count,
false, ambiguousYear);
if ( start<0 ) {
pos.index = oldStart;
return null;
}
count = 0;
}
if (interQuoteCount == 0)
{
// This indicates two consecutive quotes inside a quote,
// for example, 'o''clock'. We need to parse this as
// representing a single quote within the quote.
if (ch != text.charAt(start))
{
pos.index = oldStart;
return null;
}
++start;
count = 1; // Make it look like we never left
}
}
else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
{
// ch is a date-time pattern
if (ch != prevCh && count > 0) // e.g., yyyymmdd
{
// This is the only case where we pass in 'true' for
// obeyCount. That's because the next field directly
// abuts this one, so we have to use the count to know when
// to stop parsing. [LIU]
start = subParse(text, start, prevCh, count, true,
ambiguousYear);
if (start < 0) {
pos.index = oldStart;
return null;
}
prevCh = ch;
count = 1;
}
else
{
if (ch != prevCh)
prevCh = ch;
count++;
}
}
else if (count > 0)
{
// handle cases like: MM-dd-yy, HH:mm:ss, or yyyy MM dd,
// where ch = '-', ':', or ' ', repectively.
start=subParse(text, start, prevCh, count,
false, ambiguousYear);
if ( start < 0 ) {
pos.index = oldStart;
return null;
}
if (start >= text.length() || ch != text.charAt(start)) {
// handle cases like: 'MMMM dd' in pattern vs. "janx20"
// in time text, where ' ' doesn't match with 'x'.
pos.index = oldStart;
return null;
}
start++;
count = 0;
prevCh = 0;
}
else // any other unquoted characters
{
if (ch != text.charAt(start)) {
// handle cases like: 'MMMM dd' in pattern vs.
// "jan,,,20" in time text, where " " doesn't
// match with ",,,".
pos.index = oldStart;
return null;
}
start++;
}
++interQuoteCount;
}
}
// Parse the last item in the pattern
if (count > 0)
{
start=subParse(text, start, prevCh, count,
false, ambiguousYear);
if ( start < 0 ) {
pos.index = oldStart;
return null;
}
}
// At this point the fields of Calendar have been set. Calendar
// will fill in default values for missing fields when the time
// is computed.
pos.index = start;
// This part is a problem: When we call parsedDate.after, we compute the time.
// Take the date April 3 2004 at 2:30 am. When this is first set up, the year
// will be wrong if we're parsing a 2-digit year pattern. It will be 1904.
// April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day. 2:30 am
// is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
// on that day. It is therefore parsed out to fields as 3:30 am. Then we
// add 100 years, and get April 3 2004 at 3:30 am. Note that April 3 2004 is
// a Saturday, so it can have a 2:30 am -- and it should. [LIU]
/*
Date parsedDate = calendar.getTime();
if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) {
calendar.add(Calendar.YEAR, 100);
parsedDate = calendar.getTime();
}
*/
// Because of the above condition, save off the fields in case we need to readjust.
// The procedure we use here is not particularly efficient, but there is no other
// way to do this given the API restrictions present in Calendar. We minimize
// inefficiency by only performing this computation when it might apply, that is,
// when the two-digit year is equal to the start year, and thus might fall at the
// front or the back of the default century. This only works because we adjust
// the year correctly to start with in other cases -- see subParse().
Date parsedDate;
try {
if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
{
// We need a copy of the fields, and we need to avoid triggering a call to
// complete(), which will recalculate the fields. Since we can't access
// the fields[] array in Calendar, we clone the entire object. This will
// stop working if Calendar.clone() is ever rewritten to call complete().
Calendar savedCalendar = (Calendar)calendar.clone();
parsedDate = calendar.getTime();
if (parsedDate.before(defaultCenturyStart))
{
// We can't use add here because that does a complete() first.
savedCalendar.set(Calendar.YEAR, defaultCenturyStartYear + 100);
parsedDate = savedCalendar.getTime();
}
}
else parsedDate = calendar.getTime();
}
// An IllegalArgumentException will be thrown by Calendar.getTime()
// if any fields are out of range, e.g., MONTH == 17.
catch (IllegalArgumentException e) {
pos.index = oldStart;
return null;
}
return parsedDate;
}
/**
* Private code-size reduction function used by subParse.
* @param text the time text being parsed.
* @param start where to start parsing.
* @param field the date field being parsed.
* @param data the string array to parsed.
* @return the new start position if matching succeeded; a negative number
* indicating matching failure, otherwise.
*/
private int matchString(String text, int start, int field, String[] data)
{
int i = 0;
int count = data.length;
if (field == Calendar.DAY_OF_WEEK) i = 1;
// There may be multiple strings in the data[] array which begin with
// the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
// We keep track of the longest match, and return that. Note that this
// unfortunately requires us to test all array elements.
int bestMatchLength = 0, bestMatch = -1;
for (; i<count; ++i)
{
int length = data[i].length();
// Always compare if we have no match yet; otherwise only compare
// against potentially better matches (longer strings).
if (length > bestMatchLength &&
text.regionMatches(true, start, data[i], 0, length))
{
bestMatch = i;
bestMatchLength = length;
}
}
if (bestMatch >= 0)
{
calendar.set(field, bestMatch);
return start + bestMatchLength;
}
return -start;
}
/**
* Private member function that converts the parsed date strings into
* timeFields. Returns -start (for ParsePosition) if failed.
* @param text the time text to be parsed.
* @param start where to start parsing.
* @param ch the pattern character for the date field text to be parsed.
* @param count the count of a pattern character.
* @param obeyCount if true, then the next field directly abuts this one,
* and we should use the count to know when to stop parsing.
* @param ambiguousYear return parameter; upon return, if ambiguousYear[0]
* is true, then a two-digit year was parsed and may need to be readjusted.
* @return the new start position if matching succeeded; a negative number
* indicating matching failure, otherwise.
*/
private int subParse(String text, int start, char ch, int count,
boolean obeyCount, boolean[] ambiguousYear)
{
Number number;
int value = 0;
int i;
ParsePosition pos = new ParsePosition(0);
int patternCharIndex = -1;
if ((patternCharIndex=formatData.patternChars.indexOf(ch)) == -1)
return -start;
pos.index = start;
int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
// If there are any spaces here, skip over them. If we hit the end
// of the string, then fail.
for (;;) {
if (pos.index >= text.length()) return -start;
char c = text.charAt(pos.index);
if (c != ' ' && c != '\t') break;
++pos.index;
}
// We handle a few special cases here where we need to parse
// a number value. We handle further, more generic cases below. We need
// to handle some of them here because some fields require extra processing on
// the parsed value.
if (patternCharIndex == 4 /*HOUR_OF_DAY1_FIELD*/ ||
patternCharIndex == 15 /*HOUR1_FIELD*/ ||
(patternCharIndex == 2 /*MONTH_FIELD*/ && count <= 2) ||
patternCharIndex == 1 /*YEAR*/)
{
// It would be good to unify this with the obeyCount logic below,
// but that's going to be difficult.
if (obeyCount)
{
if ((start+count) > text.length()) return -start;
number = numberFormat.parse(text.substring(0, start+count), pos);
}
else number = numberFormat.parse(text, pos);
if (number == null) return -start;
value = number.intValue();
}
switch (patternCharIndex)
{
case 0: // 'G' - ERA
return matchString(text, start, Calendar.ERA, formatData.eras);
case 1: // 'y' - YEAR
// If there are 4 or more YEAR pattern characters, this indicates
// that the year value is to be treated literally, without any
// two-digit year adjustments (e.g., from "01" to 2001). Otherwise
// we made adjustments to place the 2-digit year in the proper
// century -- unless the given year itself is more than two characters.
if (count <= 2 && (pos.index - start) <= 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.
int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
ambiguousYear[0] = value == ambiguousTwoDigitYear;
value += (defaultCenturyStartYear/100)*100 +
(value < ambiguousTwoDigitYear ? 100 : 0);
}
calendar.set(Calendar.YEAR, value);
return pos.index;
case 2: // 'M' - MONTH
if (count <= 2) // i.e., M or MM.
{
// Don't want to parse the month if it is a string
// while pattern uses numeric style: M or MM.
// [We computed 'value' above.]
calendar.set(Calendar.MONTH, value - 1);
return pos.index;
}
else
{
// count >= 3 // i.e., MMM or MMMM
// Want to be able to parse both short and long forms.
// Try count == 4 first:
int newStart = 0;
if ((newStart=matchString(text, start, Calendar.MONTH,
formatData.months)) > 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -