Java Date Judgment

Source: Internet
Author: User

1  Packagetest8_19;2 3 ImportJava.util.Scanner;4 5  Public classCheckdate {6      Public Static voidMain (String args[]) {7Checkdate CD =Newcheckdate ();8Scanner reader =NewScanner (system.in);9          while(true) {TenSystem.out.println ("Enter a Date:"); OneString date =reader.nextline (); A             if(CD. Checkdateformat (date)) { -System.out.println ("Yes"); -}Else { theSystem.out.println ("No"); -  -             } -         } +     } -  +      Public BooleanCheckdateformat (String date) { AString regex = "[1-2]\\d{3}. [0-1] [0-9]. [0-3] [0-9] "; at  -         if(Date.matches (regex)) { -             //get Year month day of String -String year = date.substring (0, 4); - String Month; -String day = date.substring (8,10); in  -             if(Date.substring (5, 6). Equals ("0")) { tomonth = date.substring (6, 7);//0-9 Month +}Else { -month = date.substring (5, 7);//10-12 Month the             } *  $             if(Date.substring (8, 9). Equals ("0")) {Panax NotoginsengDay = date.substring (9, 10);//0-9 Day -}Else { theDay = date.substring (8, 10);//10-31 Day +             } A             //get Year month day of int the             inty =Integer.parseint (year); +             intm =Integer.parseint (month); -             intD =Integer.parseint (day); $System.out.println ("y=" +y+ "m=" +m+ "d=" +d); $  -             //if ((M >= 1 && m <=) && (d >= 1 && d <=)) { -                 Switch(m) { the                  Case2:if(Isleapyear (y)) { -                           if(d <= 29) {Wuyi                            return true; the}Else { -                            return false; Wu                           } -                           } About                           Else{if(d <= 28) { $                                return true; -}Else { -                            return false; -                           } A                           } +                      the                  Case1: -                  Case3: $                  Case5: the                  Case7: the                  Case8: the                  Case10: the                  Case12:if(d <= 31) { -                         return true; in}Else { the                         return false; the                        } About                  Case4: the                  Case6: the                  Case9: the                  Case11:if(d <= 30) { +                        return true; -}Else { the                        return false;Bayi                     } the                 default:return false; the                  -                 } -         } the         Else{ the           return false; the         } the     } -                  the      the  the      Public BooleanIsleapyear (intYear ) {94         if(year% 4 = = 0 && year%! = 0 | | year% 400 = = 0) { the             return true; the}Else { the             return false;98         } About     } - 101}

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.