朋友们,我正在尝试计算用户提供给我的两个日期和时间条目之间的差异。我使用的逻辑是下面共享的代码片段。请纠正我哪里出错并帮助解决问题。
private void CalculateDifference(String arrDate, String arrTime,
String deptDate, String deptTime) {
String arrival=arrDate+" "+arrTime;
String dept=deptDate+" "+deptTime;
System.out.println("Inside calculate diff");
System.out.println("ARRIVAL IS"+arrival);
System.out.println("DEPARTURE IS"+dept);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date1 = null;
Date date2 = null;
try {
date1 = format.parse(arrival);
date2 = format.parse(dept);
System.out.println("ARRIVAL IN TRY IS"+date1);
System.out.println("DEPARTURE IN TRY IS"+date2);
long difference = date2.getTime() - date1.getTime();
System.out.println("Time Difference is"+difference);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
LOG-CAT
09-20 11:38:57.569: I/System.out(20028): Inside calculate diff
09-20 11:38:57.569: I/System.out(20028): ARRIVAL IS30-April - 2012 11:38
09-20 11:38:57.569: I/System.out(20028): DEPARTURE IS20-Sept - 2012 11:38
09-20 11:38:57.569: W/System.err(20028): java.text.ParseException: Unparseable date: "30-April - 2012 11:38"
09-20 11:38:57.569: W/System.err(20028): at java.text.DateFormat.parse(DateFormat.java:626)
09-20 11:38:57.569: W/System.err(20028): at com.travel.tracker.Add.CalculateDifference(Add.java:84)
09-20 11:38:57.569: W/System.err(20028): at com.travel.tracker.Add.onClick(Add.java:66)
09-20 11:38:57.569: W/System.err(20028): at android.view.View.performClick(View.java:2485)
09-20 11:38:57.579: W/System.err(20028): at android.view.View$PerformClick.run(View.java:9080)
09-20 11:38:57.579: W/System.err(20028): at android.os.Handler.handleCallback(Handler.java:587)
09-20 11:38:57.579: W/System.err(20028): at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 11:38:57.579: W/System.err(20028): at android.os.Looper.loop(Looper.java:130)
09-20 11:38:57.579: W/System.err(20028): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-20 11:38:57.579: W/System.err(20028): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 11:38:57.579: W/System.err(20028): at java.lang.reflect.Method.invoke(Method.java:507)
09-20 11:38:57.579: W/System.err(20028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-20 11:38:57.579: W/System.err(20028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-20 11:38:57.579: W/System.err(20028): at dalvik.system.NativeStart.main(Native Method)