Reputation: 13
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
}
Upvotes: 0
Views: 83
Reputation: 3221
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.
Upvotes: 2