我必须将字符串“1392298553937999872”转换为int。字符串是时间戳。通常,应该可以通过使用:
将其转换为intInteger i = Integer.valueOf(1392298553937999872);
但我收到以下例外:
java.lang.NumberFormatException:对于输入字符串: “1392298553937999872”
如果我使用双倍它可以工作,但数字是错误的。那么如何将时间戳转换为int?
答案 0 :(得分:4)
将字符串转换为long。
String a="1392298553937999872";
long b= Long.parseLong(a);
答案 1 :(得分:3)
该数字大于Integer
使用Long
Long l = new Long("1392298553937999872");
答案 2 :(得分:1)
您尝试转换的数字超过Integer.MAX_VALUE
值。你最好使用BigInteger
。
BigInteger bigInteger = new BigInteger("1392298553937999872");
答案 3 :(得分:0)
您尝试转换的值大于Integer.MAX_VALUE
(2,147,483,647),您应该使用其中一种替代类型Long,BigInteger:
Long bigValue = Long.parseLong("1392298553937999872");
// ...
Long bigValue = new Long("1392298553937999872");
// ..
Long bigValue = Long.valueOf("1392298553937999872");
// ...
BigInteger bigValue = new BigInteger("1392298553937999872");
如果添加其他库并且值可能不同,您还可以使用apache commons的NumberUtils。方法NumberUtils.createNumber(String)将根据提供的输入进行调整:
Number bigValue = NumberUtils.createNumber(inputString);