转换字符串“20130310”ex。 Android中的日期格式

时间:2013-04-16 18:07:23

标签: android performance date

我正在尝试转换

字符串日期=“20130310”到android中的日期格式,我正在异常并在滚动时崩溃。

     SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            Date parsed = format.parse(pendingTrade.voteDeadline);
            System.out.println(" 2. " + NEWS_DATE_FORMAT.format(parsed));
            holder.deadline.setText("Deadline | " + NEWS_DATE_FORMAT.format(parsed));
        }
        catch(ParseException pe) {
            System.out.println("ERROR: Cannot parse \"" + pendingTrade.voteDeadline + "\"");
        }

日志是

04-16 13:54:36.410: E/AndroidRuntime(13885): java.lang.NullPointerException
04-16 13:54:36.410: E/AndroidRuntime(13885):    at     java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1001)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at java.text.DateFormat.parse(DateFormat.java:620)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at com.cbssports.nflapp.ffb.ui.FragmentPendingTrades$PendingTradesAdapter.getView(FragmentPendingTrades.java:1443)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.AbsListView.obtainView(AbsListView.java:2143)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.ListView.fillDown(ListView.java:674)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.ListView.fillGap(ListView.java:638)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4087)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer.doFrame(Choreographer.java:531)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.os.Handler.handleCallback(Handler.java:725)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.os.Looper.loop(Looper.java:137)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您获得的例外是NullPointerException。检查pendingTrade.voteDeadline的值。它可能是空的。

答案 1 :(得分:0)

试试这个

String string =“2010年1月2日”;

Date date = new SimpleDateFormat(“MMMM d,yyyy”,Locale.ENGLISH).parse(string);

的System.out.println(日期);