java嚴格驗證日期是否正確的代碼

來源:互聯網
上載者:User

標籤:void   print   合規   boolean   pac   bool   stat   eof   IV   

 1 package com.xxxx.util; 2  3 /** 4  * 輸入日期 並進行驗證格式是否正確 5  */ 6 public class FDate { 7  8     public static void main(String[] args) { 9         System.out.println(validate("2018-06-30t"));10     }11     12     /**13      * 檢查是否是閏年14      * 15      * @param year16      * @return17      */18     public static boolean run(int year) {19         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {// 是閏年20 //            System.out.print(year + "是閏年!  ");21             return true;22         } else {23             return false;24         }25     }26 27     public static boolean validate(String dateStr) {28         String msg ="";29         String[] data = new String[3];30         boolean flag = true; // 若不符合規則將值改為false31         String year = "[0-9]{4}";// 年32         String month = "[0-9]||0[0-9]||1[12]";// 月33         String day = "[0-9]||[0-2][0-9]||3[01]";// 天34         int YEAR = 0;35         String str = dateStr;// 輸入的字串36         data = str.split("[-/.+]");37         // 最基本的檢查格式 begin38         if (!data[0].matches(year)) {39             msg = "年不對";40             flag = false;41         }42         if (!data[1].matches(month)) {43             msg = "月不對";44             flag = false;45         }46         if (!data[2].matches(day)) {47             msg = "日不對";48             flag = false;49         }50         // end51         YEAR = Integer.valueOf(data[0]);52         boolean run = run(YEAR);// run 為true是閏年否則是 非閏年53         if (run) {// 閏年54             if (data[1].matches("0[2]||2")) {// 這裡是閏年的2月55                 if (!data[2].matches("0[1-9]||[1-9]||1[0-9]||2[0-9]")) {56                     flag = false;57                     msg = "2月份的天數不對";58                 }59             }60         } else {// 非閏年61             if (data[1].matches("0[2]||2")) {// 這裡是平年的2月62                 if (!data[2].matches("0[1-9]||[1-9]||1[0-9]||2[0-8]")) {63                     flag = false;64                     msg = "2月份的天數不對";65                 }66             }67         }68 69         // 下面判斷除了2月份的大小月天數70         if (data[1].matches("0[13578]||[13578]||1[02]")) {// 這裡是大月71             if (!data[2].matches("0[1-9]||[1-9]||[12][0-9]||3[01]")) {72                 flag = false;73                 msg = data[2] + " 天數不對";74             }75         } else if (data[1].matches("0[469]||[469]||11")) {// 這裡是小月76             if (!data[2].matches("0[1-9]||[1-9]||[12][0-9]||30")) {77                 flag = false;78                 msg = data[2] + " 天數不對";79             }80         }81 82         if (flag) {83             msg = "日期格式正確";84         }85         86         return flag;87     }88 89 }

 

java嚴格驗證日期是否正確的代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.