[英]Why I can't convert a String to int?
我是 Java 的新手。 我有问题。 我有一个字符串 currentTime,其当前时间的值如下:“2204201810”。 我想将此字符串转换为 Integer。 错误是:
引起:java.lang.NumberFormatException:对于输入字符串:“2204201804”
但是不知道为什么Java不能转换。 我的意思是字符串只包含数字而不是更多。
这是我的代码:
GregorianCalendar now = new GregorianCalendar();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
String currentTime = df.format(now.getTime());
currentTime = currentTime.replace(".", "");
currentTime = currentTime.replace(" ", "");
currentTime = currentTime.replace(":", "");
try {
int currentTimeInt = Integer.valueOf(currentTime);
} catch (NumberFormatException ex) {
//Error
}
您尝试转换为int
(2204201804) 的值超过了 integer 的最大容量,即 Java 中的 2147483647。 尝试使用long
代替。 甚至可能是BigInteger
,具体取决于您的需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.