DateUtil.java 1.58 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
package com.lyms.etl.util;

import java.util.Calendar;
import java.util.Date;

public class DateUtil {
public static Integer getAge(Date birth) {
if(null==birth){
return null;
}
Calendar calendar= Calendar.getInstance();
Date date=new Date();
calendar.setTime(date);

Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date);
c2.setTime(birth);
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);

int mouth1 = c1.get(Calendar.MONTH);//0当前
int mouth2 = c2.get(Calendar.MONTH);//4
int i=0;
/* if(mouth2-mouth1>=0){
int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10
int days2 = c2.get(Calendar.DAY_OF_MONTH);//1
if(days1<days2){
i=i-1;
}else if(days1>days2){
}
}else if(mouth1-mouth2<0){
i=i-1;
}*/

if(mouth2==mouth1){
int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10
int days2 = c2.get(Calendar.DAY_OF_MONTH);//1
if(days1<days2){
i=i-1;
}else if(days1>days2){
// i=i-1;
}
}else if(mouth2>mouth1){
i=i-1;
}


// if (birth == null) {
// return null;
// }
// Date now = new Date();
// long m = now.getTime() - birth.getTime();
// Long age = m / 31536000000L;
// return age.intValue();
return (Math.abs(year1 - year2)+i);
}
}