last day (16 days later) » 

9:05 PM
1
A: Define regular Time instances, get differences between them and currrent Time on Android

GreyBeardedGeekHave a look at the Joda date / time library, and specifically the LocalTime class, which represents a time of day that is not tied to a specific date. See http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalTime.html You can use methods of the LocalTime class to convert to a time in the...

 
Thanks, I Do things like this:
<code>DateTime now = new DateTime();
LocalTime object = new LocalTime(17, 05);
DateTime test = object.toDateTimeToday();
int diff = now.compareTo(test);</code>

What's your suggest to to compare current time and all available times for a single object? While loop maybe?
 
Yes, I would think that you'd have to loop over all of the 'available' times, compare each, and select the one with the smallest positive difference.
 
Thank you. Just a problem: When try to show difference comes from the comparison; The output is just ``1``. Here's the rest of the code:
<code>String a = Integer.toString(diff);
TextView timev = (TextView) findViewById(R.id.time);

timev.setText(a);</code>
 
how are you doing the comparison?
 
As shown in the first comment: int diff = now.compareTo(test);
 
9:05 PM
That's the problem - compareTo just returns -1, 0, or 1 for less than, equals, or greater than. I think you'll have to do something like long diff = test.getMillis() - now.getMillis()
 
Dear Grey I'm stuck on this for hours now; Defined an array and make a loop to find differences but it gives **java.lang.ArrayIndexOutOfBoundsException** in logcat. here's th code:
<code>DateTime now = new DateTime();
LocalTime [] aTime = {
new LocalTime(2, 05),
new LocalTime(5, 05),
new LocalTime(00, 05)
};</code> (Cont)
<code>for (int i = 0; i < aTime.length; i++) {
int j;
DateTime[] today = new DateTime[i];
for (j = 0; j <= i; j++) {
today[j] = aTime[i].toDateTimeToday();
}
if ((aTime[i].compareTo(aTime[i+1]))!= -1){
long diff = (today[j].getMillis() - now.getMillis())/ (1000*60);
String a = Long.toString(diff);
textView.append(a+"\n");

}else{
break;
}
}</code>
Should I open new question or append this to current question?
 
I'll take a look at your code, and get back to you in a little bit
 
9:37 PM
ok, here's my implementation:
import org.joda.time.DateTime;
import org.joda.time.LocalTime;

import java.util.ArrayList;
import java.util.List;

public class TimeComparitor {

    private List<LocalTime> targetTimes;

    public TimeComparitor() {
        targetTimes = new ArrayList<LocalTime>();
    }

    public void addTargetTime(LocalTime target) {
        targetTimes.add(target);
    }

    public LocalTime getClosestFutureTarget(DateTime now) {
        LocalTime result = null;
        long resultOffset = Long.MAX_VALUE;
Here's the body of the JUnit test:
        TimeComparitor comparitor = new TimeComparitor();
        DateTime now = new DateTime(2013, 5, 21, 13, 5); // 1:05PM today

        LocalTime t1 = new LocalTime(13,0);
        comparitor.addTargetTime(t1); // 1:00PM

        LocalTime t2 = new LocalTime(14,05);
        comparitor.addTargetTime(t2); // 2:05PM

        LocalTime t3 = new LocalTime(3,23);
        comparitor.addTargetTime(t3); // 3:23AM

        LocalTime closest = comparitor.getClosestFutureTarget(now);
        assertTrue("comparison failed, got " + closest, closest == t2);
 

  last day (16 days later) »