【问题标题】:Date to string Number format exception日期转字符串数字格式异常
【发布时间】: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


【解决方案1】:

这很好用:

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

【讨论】:

    【解决方案2】:
    `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);
      }
    
      } `
    

    【讨论】:

      【解决方案3】:

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

       int cnvrtq = Integer.parseInt(cnvrt);
      

      cnvrt 持有什么?

      【讨论】:

      • cnvrt 根据代码以字符串格式显示当前日期
      • 解决了错误是由于字符串包含未转换为整数的“-”字符
      【解决方案4】:
      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 值。所以只有你得到了错误。

      【讨论】:

        【解决方案5】:

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

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

        应该是

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

        【讨论】:

          【解决方案6】:
          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();

          【讨论】:

          • int cnvrtq=Integer.parseInt(cnvrt);
          • 您正在创建对象 ft2。但在下一行你写在那里 ft.format(dNow).toString();而不是 ft2.format(dNow).toString();
          猜你喜欢
          • 2019-03-22
          • 2023-03-15
          • 1970-01-01
          • 2016-11-01
          • 2018-08-30
          • 1970-01-01
          • 2013-12-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多