java.lang.NumberFormatException:对于输入字符串:“02/10/2015”

时间:2015-10-07 07:25:59

标签: java jfreechart

我尝试将日期转换为02/10/2015到字符串

我正在尝试,但我得到错误

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "02/10/2015"

这是我的代码

private Crosshair xCrosshair;
long time = ds.getX(xy.getSeriesIndex(),  xy.getItem()).longValue();
        DecimalFormat dfT   = new DecimalFormat("00");
        GregorianCalendar gc = new GregorianCalendar();
        long lDte = (long)time;
        Date dtXX = new Date(lDte);
        gc.setTime(dtXX);
        String sDD  = dfT.format(Double.valueOf(String.valueOf(gc.get(GregorianCalendar.DAY_OF_MONTH))));
        String sMM  = dfT.format(Double.valueOf(String.valueOf(gc.get(GregorianCalendar.MONTH)+1)));
        String sYY  = dfT.format(Double.valueOf(String.valueOf(gc.get(GregorianCalendar.YEAR))));
        String dateString = sDD +"/"+ sMM +"/"+ sYY;
        this.xCrosshair.setValue(Double.parseDouble(dateString));

3 个答案:

答案 0 :(得分:2)

这一行

this.xCrosshair.setValue(Double.parseDouble(dateString));

尝试将作为日期的String转换为Double(不是日期)

答案 1 :(得分:0)

您的代码无效:

String dateString = sDD +"/"+ sMM +"/"+ sYY;
this.xCrosshair.setValue(Double.parseDouble(dateString));

dateString将始终具有/,因此永远无法正确解析。

对于此用途SimpleDateFormat

DateFormat dt = new SimpleDateFormat("dd/MM/yyy");
long d = dt.parse(dateString).getTime();

答案 2 :(得分:0)

正如已经指出的那样,错误发生是因为您尝试解析包含无效字符的字符串,请参阅: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#valueOf(java.lang.String)

如果要将日期解析为double,则可能的解决方案是:

String doubleString = sDD + sMM + sYY; // Simply leave the slashes out, but is this really what you're after?
this.xCrosshair.setValue(Double.parseDouble(doubleString));