Using time on the Android platform
I have a lot of time information in hh: mm format and I was wondering if there is a good way to take this information and compare it with the current time. Let's say I have a list of times a day and I want to know what time in this array of strings is the first that has not yet passed that day.
I was looking at the Calendar API and I figured I could break the lines in hours and minutes by dividing it by ":" and then creating a calendar object from it, but that seems pretty inefficient.
Just find some input; thanks rob
<string-array name="example">
<item>6:58</item>
<item>7:41</item>
<item>8:08</item>
<item>8:28</item>
<item>8:48</item>
<item>9:08</item>
<item>9:43</item>
<item>10:13</item>
<item>10:43</item>
<item>11:13</item>
<item>11:43</item>
<item>12:09</item>
<item>12:29</item>
<item>12:49</item>
<item>1:09</item>
<item>1:29</item>
<item>1:49</item>
<item>2:09</item>
<item>2:29</item>
<item>2:49</item>
<item>3:09</item>
<item>3:29</item>
<item>3:49</item>
<item>4:09</item>
<item>4:29</item>
<item>4:49</item>
<item>5:09</item>
<item>5:29</item>
<item>5:49</item>
<item>6:29</item>
<item>7:09</item>
<item>7:47</item>
<item>8:27</item>
<item>9:07</item>
<item>9:47</item>
<item>10:27</item>
</string-array>
a source to share
Create an ArrayList that implements RandomAccess for your data. Implement a comparator on your hour: minute object. Place your array in this list for processing and sorting.
Capture the hour and minute from the current time in the usual way.
Run Collections.binarySearch () to get the index of the list item that matches or next in the string and returns it (this is the default behavior for binary search).
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.RandomAccess;
public class scratchpad {
public static void main(String [] args){
String[] hourMinuteStringArray = {"6:58","7:41","8:08","8:28","8:48","9:08","9:43","10:13","10:43","11:13","11:43","12:09","12:29","12:49","13:09","13:29","13:49","14:09","14:29","14:49","15:09","15:29","15:49","16:09","16:29","16:49","17:09","17:29","17:49","18:29","19:09","19:47","20:27","21:07","21:47","22:27"};
HourMinuteList hourMinuteList = convertHMStringArrayToHMArray(hourMinuteStringArray);
Collections.sort(hourMinuteList);
Calendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
scratchpad s = new scratchpad();
HourMinute now = s.new HourMinute(hour,minute);
System.out.println("Now is " + hour + ":" + minute);
int nearestTimeIndex = Collections.binarySearch(hourMinuteList, now);
if(nearestTimeIndex < 0){
nearestTimeIndex = nearestTimeIndex * -1 -1;
}
System.out.println("Next time is " + ((HourMinute) hourMinuteList.get(nearestTimeIndex)).getHour() + ":" + ((HourMinute) hourMinuteList.get(nearestTimeIndex)).getMinuteString());
}
private static HourMinuteList convertHMStringArrayToHMArray(String[] times){
scratchpad s = new scratchpad();
HourMinuteList list = s.new HourMinuteList();
for(String time : times ){
String[] splitTime = time.split(":");
int hour = Integer.parseInt(splitTime[0]);
int minute = Integer.parseInt(splitTime[1]);
HourMinute hm = s.new HourMinute(hour,minute);
list.add(hm);
}
return list;
}
class HourMinuteList extends ArrayList implements RandomAccess{
}
class HourMinute implements Comparable {
int hour;
int minute;
public HourMinute(int hour, int minute) {
setHour(hour);
setMinute(minute);
}
int getMinute() {
return this.minute;
}
String getMinuteString(){
if(this.minute < 10){
return "0" + this.minute;
}else{
return "" + this.minute;
}
}
int getHour() {
return this.hour;
}
void setHour(int hour) {
this.hour = hour;
}
void setMinute(int minute) {
this.minute = minute;
}
@Override
public int compareTo(Object aThat) {
if (aThat instanceof HourMinute) {
HourMinute that = (HourMinute) aThat;
if (this.getHour() == that.getHour()) {
if (this.getMinute() > that.getMinute()) {
return 1;
} else if (this.getMinute() < that.getMinute()) {
return -1;
} else {
return 0;
}
} else if (this.getHour() > that.getHour()) {
return 1;
} else if (this.getHour() < that.getHour()) {
return -1;
} else {
return 0;
}
}
return 0;
}
}
}
a source to share
Could you just do it differently? Get the current time from the Calendar in hours and minutes and convert them to strings and then compare them with strings.
If you have really a lot of data, I would expect you to be able to create a trie or something similar, and using the current time of the full string as a search key, which record matches your criteria first?
a source to share