Long.parseLong("+2013") 抛出 NumberFormatException
Long.parseLong("+2013") throws NumberFormatException
我正在尝试使用外部 Java 库来处理 Android 但在处理日期时出现错误。
我已将范围缩小到这一行:
Long year = Long.parseLong("+2013");
抛出一个
Caused by: java.lang.NumberFormatException: Invalid long: "+2013"
但是,该代码是有效的并且可以在纯 Java 应用程序中运行。为什么 Long.parseLong()
在 Android 应用程序中的工作方式不同?
Android's documentation 声明“+”Ascii 符号被识别:
public static long parseLong (String string)
Parses the specified string as a signed decimal long value. The ASCII characters - ('-') and + ('+') are recognized as the minus and plus signs.
谢谢大家!设法找出问题所在。
似乎 API 17: Android 4.2 (Jelly Bean) 引起了问题。 API 没有正确处理 parseLong() 中的“+”号。
解决了将编译 SDK 更改为 API 21 及更高版本的问题。
API 20 parseLong()
/**
* Parses the specified string as a signed long value using the specified
* radix. The ASCII character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of a long value.
* @param radix
* the radix to use when parsing.
* @return the primitive long value represented by {@code string} using
* {@code radix}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as a long value, or
* {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
*/
public static long parseLong(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null) {
throw invalidLong(string);
}
int length = string.length(), i = 0;
if (length == 0) {
throw invalidLong(string);
}
boolean negative = string.charAt(i) == '-';
if (negative && ++i == length) {
throw invalidLong(string);
}
return parse(string, i, radix, negative);
}
API 21 parseLong()
/**
* Parses the specified string as a signed long value using the specified
* radix. The ASCII characters \u002d ('-') and \u002b ('+') are recognized
* as the minus and plus signs.
*
* @param string
* the string representation of a long value.
* @param radix
* the radix to use when parsing.
* @return the primitive long value represented by {@code string} using
* {@code radix}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as a long value, or
* {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
*/
public static long parseLong(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null || string.isEmpty()) {
throw invalidLong(string);
}
char firstChar = string.charAt(0);
int firstDigitIndex = (firstChar == '-' || firstChar == '+') ? 1 : 0;
if (firstDigitIndex == string.length()) {
throw invalidLong(string);
}
return parse(string, firstDigitIndex, radix, firstChar == '-');
}
我正在尝试使用外部 Java 库来处理 Android 但在处理日期时出现错误。
我已将范围缩小到这一行:
Long year = Long.parseLong("+2013");
抛出一个
Caused by: java.lang.NumberFormatException: Invalid long: "+2013"
但是,该代码是有效的并且可以在纯 Java 应用程序中运行。为什么 Long.parseLong()
在 Android 应用程序中的工作方式不同?
Android's documentation 声明“+”Ascii 符号被识别:
public static long parseLong (String string)
Parses the specified string as a signed decimal long value. The ASCII characters - ('-') and + ('+') are recognized as the minus and plus signs.
谢谢大家!设法找出问题所在。
似乎 API 17: Android 4.2 (Jelly Bean) 引起了问题。 API 没有正确处理 parseLong() 中的“+”号。
解决了将编译 SDK 更改为 API 21 及更高版本的问题。
API 20 parseLong()
/**
* Parses the specified string as a signed long value using the specified
* radix. The ASCII character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of a long value.
* @param radix
* the radix to use when parsing.
* @return the primitive long value represented by {@code string} using
* {@code radix}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as a long value, or
* {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
*/
public static long parseLong(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null) {
throw invalidLong(string);
}
int length = string.length(), i = 0;
if (length == 0) {
throw invalidLong(string);
}
boolean negative = string.charAt(i) == '-';
if (negative && ++i == length) {
throw invalidLong(string);
}
return parse(string, i, radix, negative);
}
API 21 parseLong()
/**
* Parses the specified string as a signed long value using the specified
* radix. The ASCII characters \u002d ('-') and \u002b ('+') are recognized
* as the minus and plus signs.
*
* @param string
* the string representation of a long value.
* @param radix
* the radix to use when parsing.
* @return the primitive long value represented by {@code string} using
* {@code radix}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as a long value, or
* {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
*/
public static long parseLong(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null || string.isEmpty()) {
throw invalidLong(string);
}
char firstChar = string.charAt(0);
int firstDigitIndex = (firstChar == '-' || firstChar == '+') ? 1 : 0;
if (firstDigitIndex == string.length()) {
throw invalidLong(string);
}
return parse(string, firstDigitIndex, radix, firstChar == '-');
}