views:

55

answers:

5

table structure:

sysdurationtimeday , sysdurationtimehour, sysdurationtimeminute
1, 12,10
3, 23,10
0,  0,10

i have these 3 fields from database, after getting these 3 values, what is the technique that i can use do cast to which Java Object? (maybe Calendar.class, TimeStamp.class) ?

and use it to compared with record is spent less than 1 day, more than 1 day + less than 3 days. etc?

A: 

If you literally want "more than 1 day", that is there's no rounding so d=1, h=23, m=59 gives you 1 day, not 2 days then you can just use sysdurationtimeday and completely ignore hours and minutes. (That assumes you don't have more than 24 in sysdurationtimehour).

Classes such as Calendar don't help, they are for manipulating actual dates, you already are working in durations.

djna
A: 

Jodatime supports durations and then you get the operations needed for free.

If you are hesitant to add another dependency and learning curve, I would create a little custom class with a field storing the duration as a number in the desired precision. Then add some methods to do your comparisons or return a Calendar object or a Date aded and subtracted with your duration.

My guess is this will end up being cleaner than using the standard Java API's which always end up in complicated, clunky code when you start manipulating time.

Peter Tillemans
+1  A: 

I would prefer my own class, overriding the "essential" methods.

public class SysDuration implements Comparable {

    int day;
    int hour;
    int min;

    public SysDuration(int day,int hour,int min) {
    }

    public boolean equals(Object obj) {
    }

    public int hashCode() {
    }

    public int compareTo(Object obj) {
    }

    public boolean spendLess(SysDuration dur) {
    }

}
PeterMmm
+2  A: 

As long as you're talking durations and not absolute times, this is pretty easy. Just express the time in a convenient unit, say seconds:

time_in_seconds = 86400*sysdurationtimeday +
                  3600*sysdurationtimehour +
                  60*sysdurationtimeminute

In Java the standard way to represent this is actually as a long value in milliseconds, ala System.currentTimeMillis().

All the standard Java classes are intended to handle absolute times and need to deal with daylight savings, leap years, and all that crap. At least with the data you gave us, you don't have the required info anyway: there's no way to tell if the day was a daylight savings day and therefore took 23 or 25 hours instead of 24.

Keith Randall
+1  A: 

Lots of good answers already.

A sugegstion, perhaps out of scope, if you use durations in java I would prefer to just calculate and store this in one variable, typically a long in milliseconds if this resolution is good enough. The splitting in 3 variables usually make most of the code more complicated.

Calculations are easier and intergartion with libs such as jodatime and similar will be even more simple.

Bjorn