Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我能';t将字符串转换为int?_Java_String_Int - Fatal编程技术网

Java 为什么我能';t将字符串转换为int?

Java 为什么我能';t将字符串转换为int?,java,string,int,Java,String,Int,我对Java非常陌生。我有个问题。 我有一个字符串currentTime,它的当前时间值如下:“2204201810”。 我想把这个字符串转换成整数。 错误是: 原因:java.lang.NumberFormatException:对于输入字符串:“2204201804” 但是我不知道为什么Java不能转换它!我的意思是字符串只包含数字,而不是更多 这是我的密码: GregorianCalendar now = new GregorianCalendar(); DateFormat df = D

我对Java非常陌生。我有个问题。 我有一个字符串currentTime,它的当前时间值如下:“2204201810”。 我想把这个字符串转换成整数。 错误是:

原因: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)的值超过了最大整数容量,在Java中为2147483647。尝试改用
long
。甚至可能是
biginger
,这取决于您的需要。

您试图转换为
int
(2204201804)的值超过了最大整数容量,在Java中为2147483647。尝试改用
long
。或者甚至可能是
biginger
,这取决于您的需要。

您是否检查了
currentTime
的内容?我怀疑它是否具有您期望的内容/格式在替换前后打印字符串,您可能跳过了一些无法转换为
int
的字符。我不确定下面的答案是如何工作的。运行此代码时
System.out.println(当前时间)
输出为
4/22/201042AM
,据我所知,它无法转换为int、long或BigInteger。@hooknc,那么您的操作系统中有不同的区域设置,使用斜杠而不是点、逗号或冒号,最后还附加AM/PM。您检查了
currentTime
的内容吗?我怀疑它是否具有您期望的内容/格式在替换前后打印字符串,您可能跳过了一些无法转换为
int
的字符。我不确定下面的答案是如何工作的。运行此代码时
System.out.println(当前时间)
输出为
4/22/201042AM
,据我所知,它无法转换为int、long或BigInteger。@hooknc,那么您的操作系统中有不同的语言环境设置,使用斜杠而不是点、逗号或冒号,最后还附加AM/PM。没问题。你能接受正确的答案吗?没问题。你能接受正确的答案吗?