0

实际上,我想找出两个日期之间的差异,但在当前日期得到不可分割的日期错误:

String act_dateString;

         Calendar cal =  Calendar.getInstance();
          act_dateString  = cal.getTime().toLocaleString();


        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd");

        Date date_old = null;

            try {
                date_old = formatter1.parse(act_dateString);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Logcat 显示:

10-21 00:03:34.904: W/System.err(770): java.text.ParseException: Unparseable date: Oct 21, 2013 12:03:34 AM
10-21 00:03:34.904: W/System.err(770):  at java.text.DateFormat.parse(DateFormat.java:645)
10-21 00:03:34.914: W/System.err(770):  at com.example.datesdifference.MainActivity.showDifference(MainActivity.java:60)
10-21 00:03:34.914: W/System.err(770):  at com.example.datesdifference.MainActivity.onCreate(MainActivity.java:25)
10-21 00:03:34.914: W/System.err(770):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-21 00:03:34.914: W/System.err(770):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 00:03:34.914: W/System.err(770):  at android.os.Looper.loop(Looper.java:123)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.main(ActivityThread.java:4627)

我在谷歌上搜索过这个但无法解决这个问题。有人可以帮忙吗?

4

5 回答 5

0

您必须这样做才能解析日期。例如,在您的代码中,调用 toLocalString() 将为您提供字符串 ' Oct 20, 2013 12:57:28 AM '。这将抛出java.text.ParseException: Unparseable date: "Oct 20, 2013 12:57:28 AM您必须输入格式为“yyyy/MM/dd”的日期字符串。为此,您必须转换日期格式转换为字符串格式“yyyy/MM/dd”。然后,它将能够解析输入字符串

  String DATE_FORMAT_NOW = "yyyy/MM/dd";
    Calendar cal = Calendar.getInstance();
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
    String dateInStringFormat = sdf.format(cal.getTime());
    try {
        //parsing the date string with format "yyyy/MM/dd"
        date = sdf.parse(dateInStringFormat);
        System.out.println(date.toLocaleString());
    } catch (ParseException ex) {
        Logger.getLogger(DateTest.class.getName()).log(Level.SEVERE, null, ex);
    }

希望你明白为什么现在抛出异常。要找出两个日期之间的天数差异,

   long diffInDays = (newerDate.getTime() - olderDate.getTime())/(1000*60*60*24);
   System.out.println(diffInDays);
于 2013-10-20T19:03:56.000 回答
0
    String act_dateString;
    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd");
    Calendar cal =  Calendar.getInstance();
    Date date_old = null;
    act_dateString =formatter1.format(cal.getTime());
       try {
           date_old = formatter1.parse(act_dateString);
       } catch (ParseException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
于 2013-10-20T18:57:10.967 回答
0

可能 的输出toLocaleString与您在 中使用的格式不匹配formatter1,因此会引发异常。

你应该试着看看是什么act_dateString样子,然后你就知道出了什么问题。

于 2013-10-20T18:44:23.487 回答
0

如果您想将日历转换为日期,那么您可以使用它,我相信

Date date_old =  new Date(cal.getTimeInMillis());
于 2013-10-20T18:48:41.800 回答
0
于 2016-08-23T23:29:59.833 回答