【发布时间】:2013-09-25 16:52:20
【问题描述】:
我正在尝试使用以下代码将日期转换为字符串,然后转换为整数,然后对 int 进行一些处理,最后返回字符串,但我得到的是 NumberFormatException。
Date dNow2 = new Date( );
SimpleDateFormat ft2 =
new SimpleDateFormat ("yyyyMM");
String cnvrt=ft.format(dNow).toString();
int cnvrtq=Integer.parseInt(cnvrt);
int []cnvrtq2=new int[13];
cnvrtq2[0]=cnvrtq-1;
int l=0;
for(int w=cnvrtq2[0];w>(cnvrtq2[0]-14);w--)
{
int y=w;
y=y%100000;
y=y%1000;
y=y%100;
if(y==0)
{
w=w-88;
}
cnvrtq2[l]=w;
l++;
}
String []cnvrtqw2=new String[13];
for(int e=0;e<14;e++)
{
cnvrtqw2[e]=Integer.toString(cnvrtq2[e]);
cnvrtqw2[e]=cnvrtqw2[e].substring(0,4)+"-"+cnvrtqw2[e].substring(5,6)+"-01 00:00:00.000";
}
for(int e=0;e<14;e++)
{
System.out.println(cnvrtqw2[e]);
}
【问题讨论】:
-
你为什么要这么做?当你有足够的日期 API 时,听起来你尝试手动做一些事情
-
请隔离问题,格式化您的代码,并提供异常
-
错误在线 int cnvrtq=Integer.parseInt(cnvrt);当我隔离代码时
-
解决了错误是由于字符串包含未转换为整数的“-”字符
标签: java type-conversion numberformatexception