如何在android中将sql时间格式转换为时间

时间:2016-11-13 19:00:36

标签: java android converter datetime-format numberformatexception

我想将时间戳设置为“time ago”,例如1 m前,1天前,10天前等等,在发布我的JSON输出日期之后

{
            "id": 15,
            "image": "Null",
            "title": "Kunal",
            "status": "Bjamamnnznnsm",
            "profilepic": "Null",
            "created_at": "2016-10-15 03:50:20",
            "url": "Null"
        },

我从feeditem.java获取Volley的时间戳

 public FeedItem(int id, String name, String image, String status,
                    String profilePic, String timeStamp, String url) {
        super();
        this.id = id;
        this.name = name;
        this.image = image;
        this.status = status;
        this.profilePic = profilePic;
        this.timeStamp = timeStamp;
        this.url = url;
    }

这里我从mainactivity到setTimeStamp()获取时间戳值,并通过getTimeStamp()发送到feedlistadapter

 public String getTimeStamp() {
        return timeStamp;
    }


    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

时间戳转换和feedlistadapter:

CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(Long.parseLong(item.getTimeStamp()),new Date().getTime()
               , DateUtils.SECOND_IN_MILLIS,DateUtils.FORMAT_ABBREV_RELATIVE);
        timestamp.setText(timeAgo);

我的应用程序崩溃了

DateUtils.getRelativeTimeSpanString(Long.parseLong(item.getTimeStamp()),new Date().getTime()`

这是logcat错误

11-14 00:12:30.823 12831-12831/in.com.goalert E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: in.com.goalert, PID: 12831
                                                                java.lang.NumberFormatException: Invalid long: "2016-10-15 03:50:20"
                                                                    at java.lang.Long.invalidLong(Long.java:124)
                                                                    at java.lang.Long.parse(Long.java:363)
                                                                    at java.lang.Long.parseLong(Long.java:353)
                                                                    at java.lang.Long.parseLong(Long.java:321)
                                                                    at in.com.goalert.adapter.FeedListAdapter.getView(FeedListAdapter.java:85)
                                                                    at android.widget.AbsListView.obtainView(AbsListView.java:3065)
                                                                    at android.widget.ListView.makeAndAddView(ListView.java:1945)
                                                                    at android.widget.ListView.fillDown(ListView.java:719)
                                                                    at android.widget.ListView.fillFromTop(ListView.java:785)
                                                                    at android.widget.ListView.layoutChildren(ListView.java:1721)
                                                                    at android.widget.AbsListView.onLayout(AbsListView.java:2852)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:630)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                                                                    at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                    at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1367)
                                                                    at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:849)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
                                                                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
                                                                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
                                                                    at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3175)
                                                                    at android.view.View.layout(View.java:17995)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:5817)
                                                                    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2759)
                                                                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2460)
                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1528)
                                                                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7564)
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                    at android.view.Choreographer.doFrame(Choreographer.java:622)
                                                                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:148)

0 个答案:

没有答案