-3

我正在尝试以下代码将日期转换为字符串,然后转换为整数,然后对 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]);
    }
4

6 回答 6

1

NumberFormatException当您尝试将无效整数转换为时string调用int

int a=Integer.parseInt("a");//here you will get NumberFormatException

它应该是

int a=Integer.parseInt("5");//it works fine
于 2013-09-20T10:31:30.780 回答
0

这工作正常:

String date="20140809";
int numberDate=Integer.parseInt(date);
/* Whatever processing */
String date2=new Integer(numberDate).toString();
于 2014-09-10T19:46:23.597 回答
0
Store into cnvrtqw2[e] : 201306 2
Store into cnvrtqw2[e] : 201305 3
Store into cnvrtqw2[e] : 201304 4
Store into cnvrtqw2[e] : 201303 5
Store into cnvrtqw2[e] : 201302 6
Store into cnvrtqw2[e] : 201301 7
Store into cnvrtqw2[e] : 201212 8
Store into cnvrtqw2[e] : 0 9
    at java.lang.String.substring(String.java:1946)
    at Test.main(Test.java:74)
Java Result: 1

在这条线上

cnvrtqw2[e] = Integer.toString(cnvrtq2[e]);
            cnvrtqw2[e] = cnvrtqw2[e].substring(0, 4) + "-" + cnvrtqw2[e].substring(5, 6) + "-01      00:00:00.000";

在那个地方你得到的值是 0。但你正试图得到 subString 值。所以只有你得到了错误。

于 2013-09-20T10:31:42.833 回答
0
Date dNow2 = new Date( );
        SimpleDateFormat ft2 = 
        new SimpleDateFormat ("yyyyMM");
        String cnvrt=ft2.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;
            //System.out.println("hellllllllllllllllllllllll");

        }
        //System.out.println(w);
        cnvrtq2[l]=w;
        l++;
    }
   // try
   // {
    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]);
    }

尝试这个。String cnvrt=ft2.format(dNow).toString();我认为你在那里写的时候犯了错误String cnvrt=ft.format(dNow).toString();

于 2013-09-20T10:26:45.170 回答
0

如果您知道是哪一行引发了异常,这将有所帮助。我怀疑是这个:

 int cnvrtq = Integer.parseInt(cnvrt);

cnvrt持有什么?

于 2013-09-20T10:26:56.773 回答
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);
  }

  } `
于 2013-09-20T12:17:04.973 回答