• 通过出生日期获取年龄的方法--Java


    项目中需要根据用户填写的出生日期获得用户的年龄,一般直接写成一个方法,然后调用方法进行计算比较省事。

    下面介绍一种根据出生日期计算年龄的方法:

    package com.hyhl.util;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class AgeUtil {
    
         public static  Date parse(String strDate) throws ParseException {  
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                return sdf.parse(strDate);  
            }  
           //由出生日期获得年龄  
            public static  int getAge(Date birthDay) throws Exception {  
                Calendar cal = Calendar.getInstance();  
                if (cal.before(birthDay)) {  
                    throw new IllegalArgumentException(  
                            "The birthDay is before Now.It's unbelievable!");  
                }  
                int yearNow = cal.get(Calendar.YEAR);  
                int monthNow = cal.get(Calendar.MONTH);  
                int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);  
                cal.setTime(birthDay);   
          
                int yearBirth = cal.get(Calendar.YEAR);  
                int monthBirth = cal.get(Calendar.MONTH);  
                int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);   
          
                int age = yearNow - yearBirth;  
          
                if (monthNow <= monthBirth) {  
                    if (monthNow == monthBirth) {  
                        if (dayOfMonthNow < dayOfMonthBirth) age--;  
                    }else{  
                        age--;  
                    }  
                }  
                return age;  
            }  
            
            public static void main(String args[]) { 
                 try {
                    int  age = getAge(parse("2991-10-21"));
                    System.out.println("age=="+age);
                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
      
                  
            }
    }

    直接方法即可

    package com.hyhl.util;
    import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;
    public class AgeUtil {
    public static  Date parse(String strDate) throws ParseException {          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");          return sdf.parse(strDate);      }     //由出生日期获得年龄      public static  int getAge(Date birthDay) throws Exception {          Calendar cal = Calendar.getInstance();          if (cal.before(birthDay)) {              throw new IllegalArgumentException(                      "The birthDay is before Now.It's unbelievable!");          }          int yearNow = cal.get(Calendar.YEAR);          int monthNow = cal.get(Calendar.MONTH);          int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);          cal.setTime(birthDay);             int yearBirth = cal.get(Calendar.YEAR);          int monthBirth = cal.get(Calendar.MONTH);          int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);             int age = yearNow - yearBirth;            if (monthNow <= monthBirth) {              if (monthNow == monthBirth) {                  if (dayOfMonthNow < dayOfMonthBirth) age--;              }else{                  age--;              }          }          return age;      }          public static void main(String args[]) {      try {int  age = getAge(parse("2991-10-21"));System.out.println("age=="+age);} catch (ParseException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}             }}

  • 相关阅读:
    ireport字体无效处理
    java web调用打印机打印pdf文件
    Extjs中如何在一行textfield后面增加文字提示
    java 压缩文件
    filter加载springbean
    Elasticsearch问题记录
    dubbo管控台的安装记录及dubbo开发调试记录
    mysql索引整理
    fastdfs集群搭建3
    fastdfs集群搭建2
  • 原文地址:https://www.cnblogs.com/wyf-love-dch/p/7483094.html
Copyright © 2020-2023  润新知