StringBuffer类型强转Integer类型报错,求助
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
StringBuffer sb=new StringBuffer();
sb.append(c.get(Calendar.YEAR));
sb.append(c.get(Calendar.MONTH)+1);
sb.append(c.get(Calendar.DAY_OF_MONTH));
sb.append(c.get(Calendar.HOUR_OF_DAY));
sb.append(c.get(Calendar.MINUTE));
sb.append(c.get(Calendar.SECOND));
Integer time=Integer.parseInt(sb.toString()); //报错
System.out.println(time);
}
integer
StringBuffer
强转
------解决方案--------------------用long类型
------解决方案--------------------12位数字已经超出int的范围了
------解决方案-------------------- public static int parseInt(String s) throws
NumberFormatException {
return parseInt(s,10);
}
上面是源码,最大10位,你的12,所有报错,改为
Long time=Long.parseLong(sb.toString());
------解决方案--------------------超过范围了 结果2013328145522 已经13位了