下面列出了怎么用android.util.TimeFormatException的API类实例代码及写法,或者点击链接到github查看源代码。
private void checkChar(String s, int spos, char expected) {
char c = s.charAt(spos);
if (c != expected) {
throw new TimeFormatException(String.format(
"Unexpected character 0x%02d at pos=%d. Expected 0x%02d (\'%c\').",
(int) c, spos, (int) expected, expected));
}
}
private static int getChar(String s, int spos, int mul) {
char c = s.charAt(spos);
if (Character.isDigit(c)) {
return Character.getNumericValue(c) * mul;
} else {
throw new TimeFormatException("Parse error at pos=" + spos);
}
}
@Override
public int parsePart(String value, EventRecurrence er) {
if (VALIDATE_UNTIL) {
try {
// Parse the time to validate it. The result isn't retained.
Time until = new Time();
until.parse(value);
} catch (TimeFormatException tfe) {
throw new InvalidFormatException("Invalid UNTIL value: " + value);
}
}
er.until = value;
return PARSED_UNTIL;
}
public static Time timeFrom3339(String s3339) {
final Time time = new Time();
if (TextUtils.isEmpty(s3339)) {
return time;
}
try {
time.parse3339(s3339);
} catch (TimeFormatException e) {
return time;
}
time.switchTimezone(Time.getCurrentTimezone());
return time;
}
public static Time timeFrom3339(String s3339) {
final android.text.format.Time time = new Time();
if (TextUtils.isEmpty(s3339)) {
return time;
}
try {
time.parse3339(s3339);
} catch (TimeFormatException e) {
return time;
}
time.switchTimezone(android.text.format.Time.getCurrentTimezone());
return time;
}
@Override
public int parsePart(String value, EventRecurrence er) {
if (VALIDATE_UNTIL) {
try {
// Parse the time to validate it. The result isn't retained.
Time until = new Time();
until.parse(value);
} catch (TimeFormatException tfe) {
throw new InvalidFormatException("Invalid UNTIL value: " + value);
}
}
er.until = value;
return PARSED_UNTIL;
}
/**
* Parse a time in the current zone in YYYYMMDDTHHMMSS format.
*/
private boolean parseInternal(String s) {
int len = s.length();
if (len < 8) {
throw new TimeFormatException("String is too short: \"" + s +
"\" Expected at least 8 characters.");
}
boolean inUtc = false;
// year
int n = getChar(s, 0, 1000);
n += getChar(s, 1, 100);
n += getChar(s, 2, 10);
n += getChar(s, 3, 1);
year = n;
// month
n = getChar(s, 4, 10);
n += getChar(s, 5, 1);
n--;
month = n;
// day of month
n = getChar(s, 6, 10);
n += getChar(s, 7, 1);
monthDay = n;
if (len > 8) {
if (len < 15) {
throw new TimeFormatException(
"String is too short: \"" + s
+ "\" If there are more than 8 characters there must be at least"
+ " 15.");
}
checkChar(s, 8, 'T');
allDay = false;
// hour
n = getChar(s, 9, 10);
n += getChar(s, 10, 1);
hour = n;
// min
n = getChar(s, 11, 10);
n += getChar(s, 12, 1);
minute = n;
// sec
n = getChar(s, 13, 10);
n += getChar(s, 14, 1);
second = n;
if (len > 15) {
// Z
checkChar(s, 15, 'Z');
inUtc = true;
}
} else {
allDay = true;
hour = 0;
minute = 0;
second = 0;
}
weekDay = 0;
yearDay = 0;
isDst = -1;
gmtoff = 0;
return inUtc;
}