I'm very new in Java. I've a problem. I have the String currentTime with a value of the current time like this: "2204201810". I want to convert this String to an Integer. The Error is:
Caused by: java.lang.NumberFormatException: For input string: "2204201804"
But I don't know why Java can't convert it. I mean the String contains just Numbers not more.
Here is my code:
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
}
The value you are trying to convert to int
(2204201804) exceeds the maximum integer capacity, which is 2147483647 in Java. Try using long
instead. Or maybe even BigInteger
, depending on your needs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.