当前位置: 代码迷 >> Java相关 >> String变换为Int型 NumberFormatException
  详细解决方案

String变换为Int型 NumberFormatException

热度:3223   发布时间:2013-02-25 21:44:17.0
String转换为Int型 NumberFormatException
RangePoint RangePoint = new RangePoint();
Date nowDate = new Date();

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String now = dateFormat.format(nowDate);
if (!now.equals("")) {
System.out.println(now);
int nowTime = Integer.parseInt(now.trim());//转换出错NumberFormatException
RangePoint.getRangeToLimitSpeed(nowTime);
}
   


now 有值,不为null 或“”;

13:03:50
Exception in thread "main" java.lang.NumberFormatException: For input string: "13:03:50"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.test.cn.Deo.main(Deo.java:16)
求大神指点,为什么转换会出错。。。。

------解决方案--------------------------------------------------------
少个空格
firstTime = dateFormat.parse(dateFormat1.format(date) + " " + time[0]);

不要关心前面的日期,三个比较时间前面的日期是相同的,所以忽略,之所以加上它是为了凑个Date
明白?后面的比较日期相同的情况下就比较时间了
  相关解决方案