zoukankan      html  css  js  c++  java
  • java 计算年龄

    只需出入字符串如(19991010),调用下面的方法,就可以计算出当前的年龄:

     1 // 计算年龄
     2     public static int getage(String date1) throws ParseException {
     3         int age = 0;
     4         if (!("".equals(date1))) {
     5             SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
     6             long leapYear = (long) getLY(date1);
     7             Date birthDate = myFormat.parse(date1);
     8             Date nowdate = new Date();
     9             Date Date = myFormat.parse(myFormat.format(nowdate));
    10             age = (int) (((Date.getTime() - birthDate.getTime())
    11                     / (24 * 60 * 60 * 1000) - leapYear) / 365);
    12         }
    13         return age;
    14     }
    15 
    16     // 计算期间闰年个数
    17     public static int getLY(String data) {
    18         int leapYear = 0;
    19         if (!("".equals(data))) {
    20             SimpleDateFormat myFormat = new SimpleDateFormat("yyyy");
    21             Date date = new Date();
    22             int birthYear = Integer.parseInt(data.substring(0, 4)); // 获取出生日期,解析为Date类型
    23             int currYear = Integer.parseInt(myFormat.format(date)); // 获取当前日期
    24             for (int year = birthYear; year <= currYear; year++) {
    25                 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
    26                     leapYear++; // 从出生年到当前年,只有是闰年就+1
    27                 }
    28             }
    29         }
    30         return leapYear;
    31     }
  • 相关阅读:
    wap学习记录
    vue router
    webpack 之 缓存处理
    webpack 之 plugin
    webpack 之 loader
    babel实践
    webpack 之 webpack-dev-server自动刷新
    webpack之source map
    vue笔记
    《高性能网站建设指南》笔记
  • 原文地址:https://www.cnblogs.com/weitangmonkey/p/5160012.html
Copyright © 2011-2022 走看看