Java实现Google Maps ETA时长转换为分钟数的技术问询
Java Solution to Convert Google Maps ETA to Total Minutes
Got it, let's break down how to convert those Google Maps ETA strings into total minutes for easy comparison. The input can come in two flavors: either x hour(s) y min(s) or u day(s) v hour(s) (when hours exceed 24), and we need a reliable way to parse both into a single minute count.
Approach
- Use regular expressions to extract numeric values and their corresponding units (day, hour, min) from the input string. This handles both singular and plural forms (like "hour" vs "hours", "min" vs "mins").
- Map each unit to its minute equivalent:
- 1 day = 1440 minutes (24 * 60)
- 1 hour = 60 minutes
- 1 min = 1 minute
- Iterate through all matched value-unit pairs, calculate the total minutes, and sum them up.
- Add error handling for invalid input formats to avoid unexpected crashes.
Complete Java Implementation
import java.util.regex.Matcher; import java.util.regex.Pattern; public class EtaConverter { // Regex pattern to match numbers and their units (supports singular/plural) private static final Pattern ETA_PATTERN = Pattern.compile("(\\d+)\\s*(day|hour|min)s?", Pattern.CASE_INSENSITIVE); public static int convertEtaToMinutes(String etaString) { if (etaString == null || etaString.isBlank()) { throw new IllegalArgumentException("ETA string cannot be null or blank"); } Matcher matcher = ETA_PATTERN.matcher(etaString); int totalMinutes = 0; while (matcher.find()) { int value = Integer.parseInt(matcher.group(1)); String unit = matcher.group(2).toLowerCase(); switch (unit) { case "day": totalMinutes += value * 1440; break; case "hour": totalMinutes += value * 60; break; case "min": totalMinutes += value; break; default: throw new IllegalArgumentException("Unsupported unit found: " + unit); } } if (totalMinutes == 0) { throw new IllegalArgumentException("Invalid ETA format: " + etaString); } return totalMinutes; } // Test the converter with sample inputs public static void main(String[] args) { // Test cases System.out.println(convertEtaToMinutes("4 hours 34 mins")); // Output: 274 System.out.println(convertEtaToMinutes("1 hour 1 min")); // Output: 61 System.out.println(convertEtaToMinutes("1 day 5 hours")); // Output: 1740 System.out.println(convertEtaToMinutes("2 days 0 hours")); // Output: 2880 System.out.println(convertEtaToMinutes("0 hours 5 mins")); // Output: 5 System.out.println(convertEtaToMinutes("1 day 0 hours")); // Output: 1440 } }
Key Details Explained
- Regex Pattern: The pattern
(\\d+)\\s*(day|hour|min)s?works by:(\\d+): Captures one or more digits (the numeric value).\\s*: Matches zero or more whitespace characters (handles spaces between number and unit).(day|hour|min): Captures the unit (case-insensitive, thanks toPattern.CASE_INSENSITIVE).s?: Makes the trailing 's' optional, so it matches both "hour" and "hours", "min" and "mins".
- Error Handling: We check for null/blank input, throw exceptions for unsupported units, and validate that at least one valid value-unit pair was found (to catch completely invalid strings).
- Flexibility: The code doesn't care about the order of units—even if an input is formatted out of the usual Google Maps order (like "34 mins 4 hours"), it will still calculate the correct total minutes.
内容的提问来源于stack exchange,提问作者shivam goyal
相关产品推荐
相关产品推荐

