Java 日期到字符串数字格式异常
我正在尝试以下代码,将日期转换为字符串,然后转换为整数,然后对int进行一些处理,最后返回字符串,但是我得到了一个Java 日期到字符串数字格式异常,java,type-conversion,numberformatexception,Java,Type Conversion,Numberformatexception,我正在尝试以下代码,将日期转换为字符串,然后转换为整数,然后对int进行一些处理,最后返回字符串,但是我得到了一个NumberFormatException Date dNow2 = new Date( ); SimpleDateFormat ft2 = new SimpleDateFormat ("yyyyMM"); String cnvrt=ft.format(dNow).toString(); int cnvrtq=Inte
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]);
}
datednow2=新日期();
SimpleDataFormat ft2=
新的简化格式(“yyyyMM”);
字符串cnvrt=ft.format(dNow.toString();
int cnvrtq=Integer.parseInt(cnvrt);
int[]cnvrtq2=新int[13];
cnvrtq2[0]=cnvrtq-1;
int l=0;
对于(int w=cnvrtq2[0];w>(cnvrtq2[0]-14);w--)
{
int y=w;
y=y%100000;
y=y%1000;
y=y%100;
如果(y==0)
{
w=w-88;
}
cnvrtq2[l]=w;
l++;
}
字符串[]cnvrtqw2=新字符串[13];
对于(int e=0;eDate dNow2=new Date();
SimpleDataFormat ft2=
新的简化格式(“yyyyMM”);
字符串cnvrt=ft2.format(dNow.toString();
int cnvrtq=Integer.parseInt(cnvrt);
int[]cnvrtq2=新int[13];
cnvrtq2[0]=cnvrtq-1;
int l=0;
对于(int w=cnvrtq2[0];w>(cnvrtq2[0]-14);w--)
{
int y=w;
y=y%100000;
y=y%1000;
y=y%100;
如果(y==0)
{
w=w-88;
//System.out.println(“hellllllllllllllllll”);
}
//系统输出打印LN(w);
cnvrtq2[l]=w;
l++;
}
//试一试
// {
字符串[]cnvrtqw2=新字符串[13];
对于(int e=0;e来说,如果您知道抛出异常的是哪一行,这会有所帮助。我怀疑是这一行:
int cnvrtq = Integer.parseInt(cnvrt);
当您试图将无效整数字符串转换为int
int a=Integer.parseInt("a");//here you will get NumberFormatException
应该是
int a=Integer.parseInt("5");//it works fine
在这一行
cnvrtqw2[e] = Integer.toString(cnvrtq2[e]);
cnvrtqw2[e] = cnvrtqw2[e].substring(0, 4) + "-" + cnvrtqw2[e].substring(5, 6) + "-01 00:00:00.000";
在该位置,您将获得值0。但您正在尝试获取子字符串值。因此,只有您得到了错误。这可以正常工作:
`can you try it once...
String dob="your date String";
String dobis=null;
final DateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
final Calendar c = Calendar.getInstance();
try {
if(dob!=null && !dob.isEmpty() && dob != "")
{
c.setTime(df.parse(dob));
int month=c.get(Calendar.MONTH);
month=month+1;
dobis=c.get(Calendar.YEAR)+"-"+month+"-"+c.get(Calendar.DAY_OF_MONTH);
}
} `
String date="20140809";
int numberDate=Integer.parseInt(date);
/* Whatever processing */
String date2=new Integer(numberDate).toString();
为什么要尝试这样做?听起来像是当您有足够的日期API时尝试手动执行某些操作请隔离问题,格式化代码,并提供ExceptionMissake is on line int cnvrtq=Integer.parseInt(cnvrt);当我隔离代码时,错误是由于字符串保留“-”未转换为integercnrt shud的字符根据代码以字符串格式插入当前日期。错误是因为字符串中包含未转换为integerint cnvrtq=Integer.parseInt(cnvrt)的“-”字符;您正在创建对象ft2。但在下一行中,您将在那里写入ft.format(dNow).toString();而不是ft2.format(dNow.toString();
`can you try it once...
String dob="your date String";
String dobis=null;
final DateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
final Calendar c = Calendar.getInstance();
try {
if(dob!=null && !dob.isEmpty() && dob != "")
{
c.setTime(df.parse(dob));
int month=c.get(Calendar.MONTH);
month=month+1;
dobis=c.get(Calendar.YEAR)+"-"+month+"-"+c.get(Calendar.DAY_OF_MONTH);
}
} `
String date="20140809";
int numberDate=Integer.parseInt(date);
/* Whatever processing */
String date2=new Integer(numberDate).toString();