- package com;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- public class CheckTimeHHMM {
-
- public static void main(String[] args) {
- boolean flg = checkTime("8:00");
- boolean flg3 = checkTime("24:00");
- boolean flg1 = checkTime("8:60");
- boolean flg2 = checkTime("25:00");
- boolean flg4 = checkTime("25:0-");
- boolean flg6 = checkTime("ss:0-");
- if (flg) {
- System.out.println("8:00是正确格式");
- }
- if (flg3) {
- System.out.println("24:00是正确格式");
- }
- if (!flg1) {
- System.out.println("8:60不是正确格式");
- }
- if (!flg2) {
- System.out.println("25:00不是正确格式");
- }
- if (!flg4) {
- System.out.println("25:0-不是正确格式");
- }
- if (!flg6) {
- System.out.println("ss:0-不是正确格式");
- }
- }
-
-
- public static boolean checkHHMM(String time) {
- SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm");
- try {
- @SuppressWarnings("unused")
- Date t = dateFormat.parse(time);
- }
- catch (Exception ex) {
- return false;
- }
- return true;
- }
-
-
- public static boolean checkTime(String time) {
- if (checkHHMM(time)) {
- String[] temp = time.split(":");
- if ((temp[0].length() == 2 || temp[0].length() == 1) && temp[1].length() == 2) {
- int h,m;
- try {
- h = Integer.parseInt(temp[0]);
- m = Integer.parseInt(temp[1]);
- } catch (NumberFormatException e) {
- return false;
- }
- if (h >= 0 && h <= 24 && m <= 60 && m >= 0) {
- return true;
- }
- }
- }
- return false;
- }
-
- }
posted @
2016-12-02 11:46
kabibo
阅读(
8186)
评论()
编辑
收藏
举报