John
John

Reputation: 437

Difference in time between strings

I was working on a coding challenge where the goal was to find the smallest time difference in a list of Strings. I gave up and looked at a solution which in all makes sense, but I'm confused on the method parseTime. Can anybody please explain this method?

As I see it, the two hours compared are subtracted and then multiplied by 60 (becuase 60 min in one hour?). From that the minutes are compared and then I get lost.. Can anybody explain this as simple as possible?

    public static void main(String[] args) {
        String[] strings = {"10:00am", "11:45pm", "5:00am", "12:01am"};
        int i = TimeDifference(strings);
        System.out.println(i);
    }

    public static int TimeDifference(String[] strArr) {

        int min = Integer.MAX_VALUE;
        for (int i=0; i<strArr.length; i++) {
            for (int j=0; j<strArr.length; j++) {
                if (i != j) {
                    int time = parseTime(strArr[j], strArr[i]);
                    if (time < min) min = time;
                }
            }
        }
        return min;

    }

    private static int parseTime(String time1, String time2) {
        int time = (parseHour(time2) - parseHour(time1))*60;
        time += parseMin(time2) - parseMin(time1);
        if (isMorning(time2) != isMorning(time1)) {
            time += 12 *60;
        } else if (time <= 0 ) {
            time += 24 * 60;
        }

        return time;
    }

    private static int parseHour(String time) {
        int hour = Integer.valueOf(time.split(":")[0]);
        return ( hour == 12? 0: hour);
    }

    private static int parseMin(String time) {
        return Integer.valueOf(time.split(":")[1].replaceAll("[^0-9]",""));
    }

    private static boolean isMorning(String time) {
        String post = time.split(":")[1].replaceAll("[^a-z]","");
        return post.toLowerCase().equals("am");
    }

Upvotes: 0

Views: 121

Answers (3)

Gautham M
Gautham M

Reputation: 4935

Yes, the difference in hours are converted to minutes by multiplying by 60. Then using parseMin the difference in minutes are added to it.

Now we need it check if it is AM or PM. Example : if the times were 10:30 am and 1:35 pm. The actual time difference is +115 minutes.

After parseHour the time would have a value of -600 ((1-10)*60). Then after parseMin, time would be -595 (difference of 5 minutes added). Now we need to see if it is AM or PM. Since time1 is AM and time2 is PM, we add a difference of 12hrs. This is because the same time in AM and PM would differ by 12hrs. After this the time would be (-605 + 720) = 115 minutes.

The case when time<=0. This is when time2 is before time1. example time1 be 10:35am and time2 be 10:30am. So the question is - how much time would it take to REACH 10:30am FROM 10:35am. Since it is alreaddy 10:35am and since we cannot go back in time, we need to wait till the next day(24 hrs) to reach 10:30am. So we add 24*60 minutes to the time, which would give -5 + (24*60)

Upvotes: 1

Joachim Borup
Joachim Borup

Reputation: 21

This is about regular expressions (RegEx). Let's look at 11:45pm as an example, and break up parseTime:

int time = (parseHour(time2) - parseHour(time1))*60;

The first line makes use of parseHour, which makes use of split, which splits the string around matches of the given regular expression, ":", and then parses the string at index 0 as an integer. The string "11:45pm" is split into a string array {"11", "45pm"}, and the integer 11 is returned.

time += parseMin(time2) - parseMin(time1);

The second line makes use of parseMin, which similarly splits the given string, but parses the string at index 1 instead. However, the string "45pm" cannot be parsed as an integer, because it contains the non-digits "pm", so we use replaceAll to replace any non-digits with empty strings. replaceAll replaces each substring of this string that matches the given regular expression with the given replacement. "[^0-9]" matches any single character not present in 0-9, which is all nondigits, so "45pm".replaceAll("[^0-9]", "") returns "45", which is then parsed as an integer.

The rest has been answered by others already.

Upvotes: 2

lkatiforis
lkatiforis

Reputation: 6255

Try to debug your code in your mind. Please check my comments below:

       int minutes = parseTime("10:00am", "11:45pm");

       private static int parseTime(String time1, String time2) {
            int minutes = (parseHour(time2) - parseHour(time1))*60;//(11 - 10) * 60 = 60 minutes
            minutes += parseMin(time2) - parseMin(time1); //60 + 45 - 0 = 105 minutes
            if (isMorning(time2) != isMorning(time1)) { // 10:00am is not morning but 11:45pm is morning
                minutes += 12 *60; //so we have to add 12 hours(12 * 60 minutes)
            } else if (minutes <= 0 ) {
                minutes += 24 * 60;
            }
    
            return minutes;
        }

Upvotes: 2

Related Questions