[英]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.