Discuss / Java / 练习

练习

Topic source

何以忘言i

#1 Created at ... [Delete] [Delete and Lock User]
public class Time {
	/**
	 * 从"21:05:19"中提取时,分,秒,否则抛出IllegalArgumentException
	 */
	public static int[] parseTime(String s) {
		// FIXME:
		if(s==null) {
			throw new IllegalArgumentException();
		}
		int[] is = new int[3];
		Pattern p = Pattern.compile("(\\d{2})\\:(\\d{2})\\:(\\d{2})");
		Matcher m = p.matcher(s);
		if(m.matches()) {
			is[0] = Integer.parseInt(m.group(1));
			if(is[0]>=24) {
				throw new IllegalArgumentException();
			}
			is[1] = Integer.parseInt(m.group(2));
			if(is[1]>=60) {
				throw new IllegalArgumentException();
			}
			is[2] = Integer.parseInt(m.group(3));
			if(is[2]>=60) {
				throw new IllegalArgumentException();
			}
		}else {
			throw new IllegalArgumentException();
		}
		return is;
	}

}

  • 1

Reply