比较经过的时间(例如:08:00 am)到当前时间android [英] Compare elapsed time (eg : 08:00 am) to current time android
问题描述
我有一个数组
的时间。我每次都检索它,以便与当前时间进行比较,以便删除已用时间,并创建一个包含剩余时间的新数组
。
I am having an array
of time . I am retrieving every time from it to compare it to the current time , so that remove the elapsed time and make a new array
which contains left time.
我比较从
String timeSlots[] = {"08:00 AM to 08:30 AM", "08:30 AM to 09:00 AM", "09:00 AM to 09:30 AM", "09:30 AM to 10:00 AM", "10:00 AM to 10:30 AM", "10:30 AM to 11:00 AM", "11:00 AM to 11:30 AM", "11:30 AM to 12:00 PM", "12:00 PM to 12:30 PM", "12:30 PM to 01:00 PM", "01:00 PM to 01:30 PM", "01:30 PM to 02:00 PM", "02:00 PM to 02:30 PM", "02:30 PM to 03:00 PM", "03:00 PM to 03:30 PM", "03:30 PM to 04:00 PM", "04:00 PM to 04:30 PM", "04:30 PM to 05:00 PM", "05:00 PM to 05:30 PM", "05:30 PM to 06:00 PM", "06:00 PM to 06:30 PM", "06:30 PM to 07:00 PM", "07:00 PM to 07:30 PM", "07:30 PM to 08:00 PM", "08:00 PM to 08:30 PM", "08:30 PM to 09:00PM"};
for (int i = 0; i < timeSlots.length; i++) {
timeSlotesTemp[i] = timeSlots[i].substring(0, 8);
removeElapsedTime(timeSlotesTemp[i].toLowerCase());
}
public void removeElapsedTime(String elapsedTime) { // elapsedTime = 08:00 pm
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Date elapsedDate = dateFormat.parse(elapsedTime);
String strElapsedDate=dateFormat.format(elapsedDate);
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
String calculatedDate = dateFormat.format(currentDate);
if (new Date(strElapsedDate).compareTo(new Date(calculatedDate))<0) {
finalTimeSlotes.remove(elapsedTime);
}
else if (new Date(strElapsedDate).compareTo(new Date(calculatedDate))>0) {
finalTimeSlotes.add(elapsedTime);
}
} catch (Exception e) {
Log.e(TAG, "" + e);
}
}
我得到 java。 lang.IllegalArgumentException:Parse error:03:30 pm
,同时比较时间
推荐答案
你可以比较Date对象而不是date String。
下面是示例代码。
Why don't you compare the Date object instead of date String. Here is sample code.
public static void removeElapsedTime(String elapsedTime) { // elapsedTime = 08:00 pm
try {
Date elapsedDate = getDateFromTimeString(elapsedTime);
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
if (elapsedDate.compareTo(currentDate) < 0) {
finalTimeSlotes.remove(elapsedTime);
} else if (elapsedDate.compareTo(currentDate) > 0) {
finalTimeSlotes.add(elapsedTime);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("" + e);
}
return;
}
public static Date getDateFromTimeString(String time) {
String currentDate = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
Date date = null;
try {
date = df1.parse(currentDate + " " + time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
编辑:
原因 java.lang.IllegalArgumentException:解析错误:03:30 pm
是传递只是时间字符串来创建新的日期对象。您还需要传递Date String和Time String。
Reason for java.lang.IllegalArgumentException: Parse error: 03:30 pm
is you are passing just time String to create new Date Object. You also need to pass Date String along with Time String.
这篇关于比较经过的时间(例如:08:00 am)到当前时间android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!