|
|
|
@ -381,42 +381,47 @@ public class DateUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int getDistanceByTwoMonth(String sDate, String eDate, String s_format, String e_format){
|
|
|
|
|
Calendar startCalendar = new GregorianCalendar();
|
|
|
|
|
startCalendar.setTime(DateUtil.getStringToDateTime(sDate, s_format));
|
|
|
|
|
Calendar endCalendar = new GregorianCalendar();
|
|
|
|
|
endCalendar.setTime(DateUtil.getStringToDateTime(eDate, e_format));
|
|
|
|
|
|
|
|
|
|
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
|
|
|
|
|
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
|
|
|
|
|
|
|
|
|
|
// 생일 안 지난 경우 month -1
|
|
|
|
|
if (startCalendar.get(Calendar.MONTH) * 100 + startCalendar.get(Calendar.DATE) > endCalendar.get(Calendar.MONTH) * 100 + endCalendar.get(Calendar.DATE) )
|
|
|
|
|
diffMonth--;
|
|
|
|
|
|
|
|
|
|
int diffMonth =0;
|
|
|
|
|
if( !null2Str(sDate,"").equals("") && !null2Str(eDate,"").equals("") ) {
|
|
|
|
|
Calendar startCalendar = new GregorianCalendar();
|
|
|
|
|
startCalendar.setTime(DateUtil.getStringToDateTime(sDate, s_format));
|
|
|
|
|
Calendar endCalendar = new GregorianCalendar();
|
|
|
|
|
endCalendar.setTime(DateUtil.getStringToDateTime(eDate, e_format));
|
|
|
|
|
|
|
|
|
|
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
|
|
|
|
|
diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
|
|
|
|
|
// 생일 안 지난 경우 month -1
|
|
|
|
|
if (startCalendar.get(Calendar.MONTH) * 100 + startCalendar.get(Calendar.DATE) > endCalendar.get(Calendar.MONTH) * 100 + endCalendar.get(Calendar.DATE) )
|
|
|
|
|
diffMonth--;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return diffMonth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int getAge(String birthDate, String birth_format, String baseDate, String base_format)
|
|
|
|
|
{
|
|
|
|
|
Calendar birthCalendar = new GregorianCalendar();
|
|
|
|
|
birthCalendar.setTime(DateUtil.getStringToDateTime(birthDate, birth_format));
|
|
|
|
|
public static int getAge(String birthDate, String birth_format, String baseDate, String base_format) {
|
|
|
|
|
|
|
|
|
|
int birthYear = birthCalendar.get(Calendar.YEAR);
|
|
|
|
|
int birthMonth = birthCalendar.get(Calendar.MONTH) + 1;
|
|
|
|
|
int birthDay = birthCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
|
|
|
|
Calendar baseCalendar = new GregorianCalendar();
|
|
|
|
|
baseCalendar.setTime(DateUtil.getStringToDateTime(baseDate, base_format));
|
|
|
|
|
|
|
|
|
|
int currentYear = baseCalendar.get(Calendar.YEAR);
|
|
|
|
|
int currentMonth = baseCalendar.get(Calendar.MONTH) + 1;
|
|
|
|
|
int currentDay = baseCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
|
|
|
|
int age = currentYear - birthYear;
|
|
|
|
|
// 생일 안 지난 경우 -1
|
|
|
|
|
if (birthMonth * 100 + birthDay > currentMonth * 100 + currentDay)
|
|
|
|
|
age--;
|
|
|
|
|
|
|
|
|
|
int age = 0;
|
|
|
|
|
|
|
|
|
|
if( !null2Str(birthDate,"").equals("") && !null2Str(baseDate,"").equals("") ) {
|
|
|
|
|
Calendar birthCalendar = new GregorianCalendar();
|
|
|
|
|
birthCalendar.setTime(DateUtil.getStringToDateTime(birthDate, birth_format));
|
|
|
|
|
|
|
|
|
|
int birthYear = birthCalendar.get(Calendar.YEAR);
|
|
|
|
|
int birthMonth = birthCalendar.get(Calendar.MONTH) + 1;
|
|
|
|
|
int birthDay = birthCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
|
|
|
|
Calendar baseCalendar = new GregorianCalendar();
|
|
|
|
|
baseCalendar.setTime(DateUtil.getStringToDateTime(baseDate, base_format));
|
|
|
|
|
|
|
|
|
|
int currentYear = baseCalendar.get(Calendar.YEAR);
|
|
|
|
|
int currentMonth = baseCalendar.get(Calendar.MONTH) + 1;
|
|
|
|
|
int currentDay = baseCalendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
|
|
|
|
age = currentYear - birthYear;
|
|
|
|
|
// 생일 안 지난 경우 -1
|
|
|
|
|
if (birthMonth * 100 + birthDay > currentMonth * 100 + currentDay)
|
|
|
|
|
age--;
|
|
|
|
|
}
|
|
|
|
|
return age;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -749,12 +754,14 @@ public class DateUtil {
|
|
|
|
|
final SimpleDateFormat f = new SimpleDateFormat(pattern);
|
|
|
|
|
|
|
|
|
|
Date result = null;
|
|
|
|
|
try {
|
|
|
|
|
result = f.parse(date);
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if(date !=null && date.length() >7 ) {
|
|
|
|
|
try {
|
|
|
|
|
result = f.parse(date);
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -957,4 +964,11 @@ public class DateUtil {
|
|
|
|
|
|
|
|
|
|
return df.format(date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String null2Str(Object str, String strDefault) {
|
|
|
|
|
if(str == null || str == "null" || str.toString().length() == 0)
|
|
|
|
|
return strDefault;
|
|
|
|
|
else
|
|
|
|
|
return str.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|