Skip to content
Advertisement

How to get list of restaurant operating days with time in a week from the API response string [closed]

Getting the response from API as below

{
  "name": "Restaurant name",
  "operatingHours": "Mon-Thu, Sun 11:30 am - 10:00 pm  / Fri-Sat 11:30 am - 10:30 pm"
},
{
  "name": "Restaurant name",
  "operatingHours": "Mon-Sat 11:00 am - 11:00 pm  / Sun 11:00 am - 10:30 pm"
}

The output list should be like below

Mon 11:30 am - 10:00 pm,
Tue 11:30 am - 10:00 pm,
Wed 11:30 am - 10:00 pm,
Thu 11:30 am - 10:00 pm,
Fri 11:30 am - 10:30 pm,
Sat 11:30 am - 10:30 pm,
Sun 11:30 am - 10:00 pm

Advertisement

Answer

Assuming operatingHours is always going to be in one of the formats given below:

  1. Mon-Thu, Sun 11:30 am - 10:00 pm / Fri-Sat 11:30 am - 10:30 pm
  2. Mon-Sat 11:00 am - 11:00 pm / Sun 11:00 am - 10:30 pm

given below can be an alternative solution:

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        // Test
        String operatingHours = "Mon-Thu, Sun 11:30 am - 10:00 pm \/ Fri-Sat 11:30 am - 10:30 pm";
        System.out.println(getOpenDaysWithTime(operatingHours));

        System.out.println();

        operatingHours = "Mon-Sat 11:00 am - 11:00 pm  \/ Sun 11:00 am - 10:30 pm";
        System.out.println(getOpenDaysWithTime(operatingHours));
    }

    static String getOpenDaysWithTime(String operatingHours) {
        String[] daysArr = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
        List<String> daysList = Arrays.asList(daysArr);
        try {
            // Split the parameter string
            String[] arr = operatingHours.split("\\/");

            // Regex to find the time range e.g. 11:30 am - 10:00 pm
            String durationRegex = "\d+:\d+ (?i)(?:[ap]m) - \d+:\d+ (?i)(?:[ap]m)";

            Pattern pattern = Pattern.compile(durationRegex);
            for (String e : arr) {
                Matcher matcher = pattern.matcher(e);
                if (matcher.find()) {
                    String durationStr = matcher.group();
                    if (durationStr != null) {
                        // String before the time range e.g. Mon-Thu, Sun -OR- Fri-Sat -OR- Sun
                        String daysStr = e.substring(0, e.indexOf(durationStr)).trim();

                        String[] parts = daysStr.trim().split(", ");

                        // Split the first element e.g. Mon-Thu -OR- Fri-Sat
                        String[] days = parts[0].split("-");

                        if (days.length > 1) {
                            for (int i = daysList.indexOf(days[0]); i <= daysList.indexOf(days[1]); i++) {
                                daysArr[i] = daysList.get(i) + " " + durationStr;
                            }
                        } else {
                            daysArr[daysList.indexOf(parts[0])] = parts[0] + " " + durationStr;
                        }

                        if (parts.length == 2) {
                            daysArr[daysList.indexOf(parts[1])] = parts[1] + " " + durationStr;
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("Error occured while processing: " + operatingHours);
            daysArr = new String[] {};
        }
        return String.join("," + System.lineSeparator(), daysArr);
    }
}

Output:

Mon 11:30 am - 10:00 pm,
Tue 11:30 am - 10:00 pm,
Wed 11:30 am - 10:00 pm,
Thu 11:30 am - 10:00 pm,
Fri 11:30 am - 10:30 pm,
Sat 11:30 am - 10:30 pm,
Sun 11:30 am - 10:00 pm

Mon 11:00 am - 11:00 pm,
Tue 11:00 am - 11:00 pm,
Wed 11:00 am - 11:00 pm,
Thu 11:00 am - 11:00 pm,
Fri 11:00 am - 11:00 pm,
Sat 11:00 am - 11:00 pm,
Sun 11:00 am - 10:30 pm

Here is the demo of the regex used to find the time range.

Explanation of the regex:

  1. d+ matches one or more digit(s)

  2. : matches the character : literally

  3. (?i) specifies case-insensitive match

  4. Non-capturing group (?:[ap]m)

    • [ap] matches a single character out of a and p
    • m matches the character m literally
  5. - matches the characters - literally

Advertisement