1. package com;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. /**
  5. * @author Gerrard
  6. */
  7. public class CheckTimeHHMM {
  8. public static void main(String[] args) {
  9. boolean flg = checkTime("8:00");
  10. boolean flg3 = checkTime("24:00");
  11. boolean flg1 = checkTime("8:60");
  12. boolean flg2 = checkTime("25:00");
  13. boolean flg4 = checkTime("25:0-");
  14. boolean flg6 = checkTime("ss:0-");
  15. if (flg) {
  16. System.out.println("8:00是正确格式");
  17. }
  18. if (flg3) {
  19. System.out.println("24:00是正确格式");
  20. }
  21. if (!flg1) {
  22. System.out.println("8:60不是正确格式");
  23. }
  24. if (!flg2) {
  25. System.out.println("25:00不是正确格式");
  26. }
  27. if (!flg4) {
  28. System.out.println("25:0-不是正确格式");
  29. }
  30. if (!flg6) {
  31. System.out.println("ss:0-不是正确格式");
  32. }
  33. }
  34. /**
  35. * 校验时间格式(仅格式)
  36. */
  37. public static boolean checkHHMM(String time) {
  38. SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm");
  39. try {
  40. @SuppressWarnings("unused")
  41. Date t = dateFormat.parse(time);
  42. }
  43. catch (Exception ex) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * 校验时间格式HH:MM(精确)
  50. */
  51. public static boolean checkTime(String time) {
  52. if (checkHHMM(time)) {
  53. String[] temp = time.split(":");
  54. if ((temp[0].length() == 2 || temp[0].length() == 1) && temp[1].length() == 2) {
  55. int h,m;
  56. try {
  57. h = Integer.parseInt(temp[0]);
  58. m = Integer.parseInt(temp[1]);
  59. } catch (NumberFormatException e) {
  60. return false;
  61. }
  62. if (h >= 0 && h <= 24 && m <= 60 && m >= 0) {
  63. return true;
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69. }

java校验时间格式 HH:MM的更多相关文章

  1. Excel将秒转换成标准的时间格式HH:MM:SS

    Excel将秒转换成标准的时间格式HH:MM:SS 比如120秒,转换成00:02:00 Excel公式为: =TEXT(A1/86400,"[hh]:mm:ss") A1为秒数据 ...

  2. jQuery中校验时间格式的正则表达式小结

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. JAVA中日期 yyyy-MM-dd HH:mm:ss和yyyy-MM-dd hh:mm:ss的区别

    JAVA中日期 yyyy-MM-dd HH:mm:ss和yyyy-MM-dd hh:mm:ss的区别 : HH:24小时制 hh:12小时制 package time; import java.tex ...

  4. java时间"yyyy-mm-dd HH:mm:ss"转成Date

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time="1 ...

  5. java获取时间格式

    文章来源:https://www.cnblogs.com/hello-tl/p/9263602.html package com.util; import java.text.SimpleDateFo ...

  6. Java中时间格式处理,指定N天/小时等之后的时间

    1)根据当前时间,获取具体的时刻的时间 N天前 M小时之前 可用 new Date().getTime() - 24 * 60 * 60 * 1000*N[N天之前]的方法来获取处理时间之后的具体的值 ...

  7. java格式化时间格式

    System.out.println("Hello World!"); SimpleDateFormat format = new SimpleDateFormat( " ...

  8. js获取当前日期时间“yyyy-MM-dd HH:MM:SS”

    获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS” 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function getNowFormatDat ...

  9. [原]时间格式化hh:mm:ss和HH:mm:ss区别

    hh:mm:ss   按照12小时制的格式进行字符串格式化 如果时间处于00:00:00——12:59:59,则返回的字符串正常 如果时间处于13:00:00——23:59:59,则返回的字符串是实际 ...

随机推荐

  1. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  2. ssh配置文件ssh_config和sshd_config区别

    问题描述:在一次配置ssh端口和秘钥登录过程中,修改几次都没有成功.最后发现修改的是ssh.config,原因是习惯tab一下,实在是眼拙! ssh_config和sshd_config配置文件区别: ...

  3. iOS不得姐项目--封装状态栏指示器(UIWindow实现)

    一.头文件 #import <UIKit/UIKit.h> @interface ChaosStatusBarHUD : NSObject /** 显示成功信息 */ + (void)sh ...

  4. Django-Model 使用

    添加 更改 删除 查找 /1 使用get 方式查找,只有一条数据,并如果数据不存在的时候会报错,建议不用 User.objects.get(uname=xxx) User. 模型 get(uname= ...

  5. js_RGB转16进制(rgb2hex)

    输入:rgb(13,0,255) 输出:#0d00ff 在线颜色转换工具:http://www.atool.org/colorpicker.php 1 2 3 4 5 6 7 8 9 function ...

  6. 【POJ 2482】Stars in Your Window

    http://poj.org/problem?id=2482 线段树扫描线 #include<cstdio> #include<cstring> #include<alg ...

  7. Web前端性能优化教程08:配置ETag

    本文是Web前端性能优化系列文章中的第五篇,主要讲述内容:配置ETag.完整教程可查看:Web前端性能优化 什么是ETag? 实体标签(EntityTag)是唯一标识了一个组件的一个特定版本的字符串, ...

  8. [分类算法] :SVM支持向量机

    Support vector machines 支持向量机,简称SVM 分类算法的目的是学会一个分类函数或者分类模型(分类器),能够把数据库中的数据项映射给定类别中的某一个,从而可以预测未知类别. S ...

  9. Leetcode 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  10. 【caffe】loss function、cost function和error

    @tags: caffe 机器学习 在机器学习(暂时限定有监督学习)中,常见的算法大都可以划分为两个部分来理解它 一个是它的Hypothesis function,也就是你用一个函数f,来拟合任意一个 ...