Java 字符串转整数的数字格式异常
Java Number Format Exception in Conversion from String to Integer
以下程序使用日期和时间生成唯一键(使用 joda 时间 API)
import org.apache.commons.codec.binary.Base64;
import org.joda.time.*;
public class EnDecoding {
public String EncodeRecieverAddress(String emailaddress){
byte[] encodedBytes = Base64.encodeBase64(emailaddress.getBytes());
return new String(encodedBytes);
}
public String DecodeRecieverAddress(String encodedemail){
byte[] decodedBytes = Base64.decodeBase64(encodedemail.getBytes());
return new String(decodedBytes);
}
public int GenerateUniquekey() {
LocalTime localtime = new LocalTime();
LocalDate localdate = new LocalDate();
String key = "" + localdate.getDayOfYear()
+ localdate.getDayOfMonth()
+ localdate.getDayOfWeek()
+ localtime.getHourOfDay()
+ localtime.getMinuteOfHour()
+ localtime.getSecondOfMinute()
+ localtime.getMillisOfSecond();
System.out.println(key);
System.out.println(Integer.parseInt(key.trim()));
return 0;
}
}
System.out.println(key);
输出:117275232750437
System.out.println(Integer.parseInt(key.trim()));
java.lang.NumberFormatException:对于输入字符串:“117275232750437”
我已经使用 id.trim() 函数来消除前导和尾随空格,但这也没有解决我的问题。
请不要将此问题标记为重复,因为其他类似类型的问题对我帮助不大,这就是我创建这个新问题的原因,因此我希望在这里得到最佳答案。
整数的最大值为2,147,483,647,您的输入太大。请改用 Long
。
使用 Long
而不是整数,因为它超出了 Integer
的范围
String s = "117275232750437";
System.out.println(Long.parseLong(s));
- 整数范围:-2,147,483,648 到 2,147,483,647
- 长范围:-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
你的数字太大了,你应该使用Long
。
int
的最大值为 2.147.483.647.
以下程序使用日期和时间生成唯一键(使用 joda 时间 API)
import org.apache.commons.codec.binary.Base64;
import org.joda.time.*;
public class EnDecoding {
public String EncodeRecieverAddress(String emailaddress){
byte[] encodedBytes = Base64.encodeBase64(emailaddress.getBytes());
return new String(encodedBytes);
}
public String DecodeRecieverAddress(String encodedemail){
byte[] decodedBytes = Base64.decodeBase64(encodedemail.getBytes());
return new String(decodedBytes);
}
public int GenerateUniquekey() {
LocalTime localtime = new LocalTime();
LocalDate localdate = new LocalDate();
String key = "" + localdate.getDayOfYear()
+ localdate.getDayOfMonth()
+ localdate.getDayOfWeek()
+ localtime.getHourOfDay()
+ localtime.getMinuteOfHour()
+ localtime.getSecondOfMinute()
+ localtime.getMillisOfSecond();
System.out.println(key);
System.out.println(Integer.parseInt(key.trim()));
return 0;
}
}
System.out.println(key);
输出:117275232750437
System.out.println(Integer.parseInt(key.trim()));
java.lang.NumberFormatException:对于输入字符串:“117275232750437”
我已经使用 id.trim() 函数来消除前导和尾随空格,但这也没有解决我的问题。
请不要将此问题标记为重复,因为其他类似类型的问题对我帮助不大,这就是我创建这个新问题的原因,因此我希望在这里得到最佳答案。
整数的最大值为2,147,483,647,您的输入太大。请改用 Long
。
使用 Long
而不是整数,因为它超出了 Integer
String s = "117275232750437";
System.out.println(Long.parseLong(s));
- 整数范围:-2,147,483,648 到 2,147,483,647
- 长范围:-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
你的数字太大了,你应该使用Long
。
int
的最大值为 2.147.483.647.