gpt4 book ai didi

java - 当 Android 中其中一个日期为字符串格式时,如何比较日期?

转载 作者:行者123 更新时间:2023-12-02 07:51:37 26 4
gpt4 key购买 nike

我对 Android 非常陌生。我从服务器获取以下格式的字符串形式的日期

2012-08-17 00:00:00

我想将此字符串与当前日期进行比较,以找出年、月和日形式的日期之间的差异。

我尝试在下面的代码中尝试解决它

Date currentDate = new Date(System.currentTimeMillis());
Log.v("@@@@@@@@@","Current Date: " + currentDate);
Date passDate = new SimpleDateFormat().parse(passDateString);
Log.v("@@@@@@@@@","Pass Date: " + passDate);
dateDifference = passDate.compareTo(currentDate);

但返回时出现以下异常

04-15 12:08:29.101: V/@@@@@@@@@(1161): Current Date: Sun Apr 15 12:08:29 GMT+01:00 2012
04-15 12:08:29.101: W/System.err(1161): java.text.ParseException: Unparseable date: 2012-08-17 00:00:00
04-15 12:08:29.111: W/System.err(1161): at java.text.DateFormat.parse(DateFormat.java:645)
04-15 12:08:29.111: W/System.err(1161): at org.apis.PassesListItemAdapter.getView(PassesListItemAdapter.java:77)
04-15 12:08:29.111: W/System.err(1161): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-15 12:08:29.111: W/System.err(1161): at android.widget.ListView.makeAndAddView(ListView.java:1727)
04-15 12:08:29.111: W/System.err(1161): at android.widget.ListView.fillDown(ListView.java:652)
04-15 12:08:29.111: W/System.err(1161): at android.widget.ListView.fillFromTop(ListView.java:709)
04-15 12:08:29.111: W/System.err(1161): at android.widget.ListView.layoutChildren(ListView.java:1580)
04-15 12:08:29.111: W/System.err(1161): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
04-15 12:08:29.111: W/System.err(1161): at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
04-15 12:08:29.111: W/System.err(1161): at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-15 12:08:29.111: W/System.err(1161): at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-15 12:08:29.111: W/System.err(1161): at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
04-15 12:08:29.111: W/System.err(1161): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
04-15 12:08:29.111: W/System.err(1161): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 12:08:29.111: W/System.err(1161): at android.os.Looper.loop(Looper.java:144)
04-15 12:08:29.111: W/System.err(1161): at android.app.ActivityThread.main(ActivityThread.java:4937)
04-15 12:08:29.111: W/System.err(1161): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 12:08:29.111: W/System.err(1161): at java.lang.reflect.Method.invoke(Method.java:521)
04-15 12:08:29.111: W/System.err(1161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-15 12:08:29.111: W/System.err(1161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-15 12:08:29.111: W/System.err(1161): at dalvik.system.NativeStart.main(Native Method)

最佳答案

添加要解析的日期格式:

 passDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-08-17 00:00:00");

关于java - 当 Android 中其中一个日期为字符串格式时,如何比较日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10161562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com