Discuss / Java / 练习

练习

Topic source

public static int[] parseTime(String s) {

        if (s == null) {

            throw new IllegalArgumentException();

        }

        Pattern pattern = Pattern.compile("([0-1]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)");

        Matcher matcher = pattern.matcher(s);

        if (matcher.matches()) {

            int hour = Integer.parseInt(matcher.group(1));

            int minute = Integer.parseInt(matcher.group(2));

            int second = Integer.parseInt(matcher.group(3));

            return new int[] {hour, minute, second};

        } else {

            throw new IllegalArgumentException();

        }

    }


  • 1

Reply