Java Computing Age

Source: Internet
Author: User

Importorg.junit.Test;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;/** * @authorCeshi * @Title: Junitage * @ProjectName Ceshi * @Description: TODO * @date 2018/6/2721:00*/ Public classjunitage {simpledateformat format=NewSimpleDateFormat ("Yyyy-mm-dd"); @Test Public voidTestage ()throwsexception{intAge = Getage (Format.parse ("1993-06-27"));    System.out.println (age); }    //Age by date of birth     Public  intGetage (Date BirthDay)throwsException {intAge = 0; Calendar Cal=calendar.getinstance (); if(Cal.before (BirthDay)) {returnAge ; }        intYearnow =Cal.get (calendar.year); intMonthnow =Cal.get (Calendar.month); intDaynow =Cal.get (Calendar.day_of_month);        Cal.settime (BirthDay); intYearbirth =Cal.get (calendar.year); intMonthbirth =Cal.get (Calendar.month); intDaybirth =Cal.get (Calendar.day_of_month); //Year SubtractionAge = Yearnow-Yearbirth; //Judging the month        if(Monthnow <=Monthbirth) {            if(Monthnow = =Monthbirth) {                //Judge Day                if(Daynow <Daybirth) { Age--; }            }Else{ Age--; }        }        returnAge ; }}

Java Computing Age

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.