Discuss / Java / 获取时分秒

获取时分秒

Topic source
public class Main {
    public static void main(String[] args) {
        String str = "23:01:59";
        Pattern p = Pattern.compile("([0-1]\\d|2[0-3]):([0-5]\\d):([0-5]\\d)");
        Matcher matcher = p.matcher(str);
        if (matcher.matches()) {
            String hou = matcher.group(1);
            String min = matcher.group(2);
            String sec = matcher.group(3);
            System.out.printf("%s时%s分%s秒", hou, min, sec);
        } else {
            System.out.println("格式不匹配");
        }
    }
}

  • 1

Reply