tags:

views:

94

answers:

2

Hey Everyone,

I have a lot of time information in the format of hh:mm and I was wondering if there is a good way to take this information and compare it to the current time. Say I have a list of times for a day and I want to find out which time in this array of strings is the first that has not already passed in this day.

I was looking at the Calendar API and I figured I could break the strings up into hours and minutes by splitting it at the ":" and then create a calendar object from that but that seems rather inefficient.

Just looking for 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>
+1  A: 

Couldn't you just do it the other way? Get the current time from Calendar in hours and minutes, and convert them to strings, and then compare those to your strings.

If your really have a lot of data, I expect you could build a trie or something similar, and using the current time's full string as the key for looking up which entry is the first to match your criteria?

Mikael Ohlson
+3  A: 

Create a ArrayList that implements RandomAccess for your data. Implement a Comparator on your hour:minute object. Put your array into this list for processing and sort it.

Grab the hour and minute from the current time in the normal fashion.

Execute a Collections.binarySearch() to get the index of the list item that matches or is next in line and return it (this is binarySearch's default behavior).

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;
    }

}

}

Ichorus
Thanks for the code. I definitely could have figured it out but you saved me some time. I had to make some changes but what you suggested was spot on. Thanks!
Tarmon