Java的NumberFormatException异常在SimpleDateFormat.getDateInstance() [英] Java NumberFormatException in SimpleDateFormat.getDateInstance()

查看:271
本文介绍了Java的NumberFormatException异常在SimpleDateFormat.getDateInstance()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白的NumberFormatException的这个code的原因:

I can't understand the reason of NumberFormatException in this code:

SimpleDateFormat format = (SimpleDateFormat) SimpleDateFormat.getDateInstance();

下面是我的LogCat中输出:

Below is my LogCat output:

10-30 18:04:05.600: W/System.err(23899): java.lang.NumberFormatException: Invalid int: ""
10-30 18:04:05.600: W/System.err(23899):    at java.lang.Integer.invalidInt(Integer.java:138)
10-30 18:04:05.600: W/System.err(23899):    at java.lang.Integer.parseInt(Integer.java:359)
10-30 18:04:05.600: W/System.err(23899):    at java.lang.Integer.parseInt(Integer.java:332)
10-30 18:04:05.600: W/System.err(23899):    at java.util.Calendar.getHwFirstDayOfWeek(Calendar.java:807)
10-30 18:04:05.600: W/System.err(23899):    at java.util.Calendar.<init>(Calendar.java:745)
10-30 18:04:05.600: W/System.err(23899):    at java.util.GregorianCalendar.<init>(GregorianCalendar.java:338)
10-30 18:04:05.600: W/System.err(23899):    at java.util.GregorianCalendar.<init>(GregorianCalendar.java:314)
10-30 18:04:05.608: W/System.err(23899):    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:378)
10-30 18:04:05.608: W/System.err(23899):    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:368)
10-30 18:04:05.608: W/System.err(23899):    at java.text.DateFormat.getDateInstance(DateFormat.java:462)
10-30 18:04:05.608: W/System.err(23899):    at java.text.DateFormat.getDateInstance(DateFormat.java:443)
10-30 18:04:05.608: W/System.err(23899):    at java.text.DateFormat.getDateInstance(DateFormat.java:426)
10-30 18:04:05.608: W/System.err(23899):    at com.mycompany.mypackage.InboxFragment$15.setViewValue(InboxFragment.java:396)

编辑:
同样的例外日期格式FORMAT = DateFormat.getDateInstance();

推荐答案

.getDateInstance从日期格式继承,因此回报的DateFormat。日期格式为SimpleDateFormat的一个超类。你不能强制转换一个超类的一个子类。这是什么你试图在这里做,这就是为什么你收到此错误。编译器会信任,当你将它转换为SimpleDateFormat的程序员,这就是为什么你不能在编译期间得到任何错误。

.getDateInstance is inherited from DateFormat, and as such returns a DateFormat. DateFormat is a superclass of SimpleDateFormat. You can't typecast a superclass to one of its subclasses. This is what you are trying to do here and it is why you are getting this error. The compiler will trust the programmer when you cast it to SimpleDateFormat, and this is why you aren't getting any error during compilation.

要短,你不能一个子类分配给它的父类的一个实例。有若干可以攻击这个问题的方法,但在这种情况下,你只是打破继承。不要总是相信Eclipse提供的快速修复;)

To be short, you can not assign a subclass to an instance of its superclass. There are a number of ways you could attack this problem, but in this case you are simply breaking inheritance. Do not always trust the quickfixes provided by eclipse ;)

这篇关于Java的NumberFormatException异常在SimpleDateFormat.getDateInstance()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆