Date to string数字格式异常

时间:2013-09-20 10:22:20

标签: java type-conversion numberformatexception

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

6 个答案:

答案 0 :(得分:1)

当您尝试将无效的整数NumberFormatException转换为string

时,

int会调用

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

应该是

int a=Integer.parseInt("5");//it works fine

答案 1 :(得分: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();

时的错误

答案 2 :(得分:0)

如果你告诉哪一行抛出异常会有所帮助。我怀疑是这一个:

 int cnvrtq = Integer.parseInt(cnvrt);

cnvrt持有什么?

答案 3 :(得分: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值。所以只有你得到错误。

答案 4 :(得分: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);
  }

  } `

答案 5 :(得分:0)

这很好用:

String date="20140809";
int numberDate=Integer.parseInt(date);
/* Whatever processing */
String date2=new Integer(numberDate).toString();