JAva年齢の計算
2778 ワード
(1991010)のような文字列に出入りするだけで、次の方法を呼び出すと、現在の年齢を計算できます.
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 }