views:

104

answers:

0

Hi,

I need to solve below using java 6 api.

The inputs are start time , end time and a given time t in a given time zone . All Times must be in 24 hr format = HH:mm

The program must return values x such that:

f(t)=  0  ,  for  start<=t<=end

    =  x  ,  for  t outside start and end where x is the 
             interval in seconds for t to reach start time.

any help appreciated.

thank you.

EDIT:I was slightly confused / afraid due to time zone but i think that does not matter , am i right?

Here is what i got so far now not sure if it is fully correct though.

public static void main(String[] a) {
        int st_hr = 1;
        int st_min = 0;
        int end_hr = 5;
        int end_min = 0;
        int curr_hr = 12;
        int curr_min = 0;
        long x = 0;
        long st_sec_index = st_hr * 60 * 60 + st_min * 60;
        long end_sec_index = end_hr * 60 * 60 + end_min * 60;
        long curr_sec_index = curr_hr * 60 * 60 + curr_min * 60;
        if (st_hr <= end_hr) {
            // span in same day

            if ((curr_sec_index - st_sec_index) >= 0 && (end_sec_index - curr_sec_index) >= 0) {
                x = 0;
            } else {
                x = Math.abs(st_sec_index - curr_sec_index);
            }
        } else {
            // span across day
            if (curr_sec_index >= st_sec_index || curr_sec_index <= end_sec_index) {
                // within window
                x = 0;
            } else {
                x = Math.abs(curr_sec_index - st_sec_index);
            }
        }
        System.out.println(x);
    }