Java Check time format hh:mm

Source: Internet
Author: User
Tags dateformat

  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 is the correct format");
  17. }
  18. if (flg3) {
  19. System.out.println ("24:00 is the correct format");
  20. }
  21. if (!FLG1) {
  22. System.out.println ("8:60 is not the correct format");
  23. }
  24. if (!flg2) {
  25. System.out.println ("25:00 is not the correct format");
  26. }
  27. if (!FLG4) {
  28. System.out.println ("25:0-is not the correct format");
  29. }
  30. if (!flg6) {
  31. System.out.println ("ss:0-is not the correct format");
  32. }
  33. }
  34. /** 
  35. * Check time format (format only)
  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. * Check time format hh:mm (exact)
  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 <= && m <= && m >= 0) {
  63. return true;
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69. }

Java Check time format hh:mm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.