-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無効な整数stringint

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
`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 に答える
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

これはうまくいきます:

String date="20140809";
int numberDate=Integer.parseInt(date);
/* Whatever processing */
String date2=new Integer(numberDate).toString();
于 2014-09-10T19:46:23.597 に答える
0

例外をスローするのはどの行かを教えていただけると助かります。私はそれがこれだと思う:

 int cnvrtq = Integer.parseInt(cnvrt);

何がcnvrt保持されますか?

于 2013-09-20T10:26:56.773 に答える
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 に答える