public class Age{
private static Date time;
public static void main(String[] args) {
try {
time = stringToDate("1966-08-03", "yyyy-MM-dd");
getAge(time);
System.out.println("年龄是" + getAge(time));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// string类型转换为date类型
// strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH时mm分ss秒,
// strTime的时间格式必须要与formatType的时间格式相同
public static Date stringToDate(String strTime, String formatType)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
date = formatter.parse(strTime);
return date;
}
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;
// 当前月份与出生日期的月份相比,如果月份小于出生月份,则年龄上减1,表示不满多少周岁
if (monthNow <= monthBirth) {
// 如果月份相等,在比较日期,如果当前日,小于出生日,也减1,表示不满多少周岁
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth)
age--;
} else {
age--;
}
}
System.out.println("age:" + age);
return age;
}
//
类型之间的转换
<pre name="code" class="html"> // date类型转换为String类型
// formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
// data Date类型的时间
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long类型转换为String类型
// currentTime要转换的long类型的时间
// formatType要转换的string类型的时间格式
public static String longToString(long currentTime, String formatType)
throws ParseException {
Date date = longToDate(currentTime, formatType); // long类型转成Date类型
String strTime = dateToString(date, formatType); // date类型转成String
return strTime;
}
// long转换为Date类型
// currentTime要转换的long类型的时间
// formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
public static Date longToDate(long currentTime, String formatType)
throws ParseException {
Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
return date;
}
// string类型转换为long类型
// strTime要转换的String类型的时间
// formatType时间格式
// strTime的时间格式和formatType的时间格式必须相同
public static long stringToLong(String strTime, String formatType)
throws ParseException {
Date date = stringToDate(strTime, formatType); // String类型转成date类型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date类型转成long类型
return currentTime;
}
}
// date类型转换为long类型
// date要转换的date类型的时间
public static long dateToLong(Date date) {
return date.getTime();
}
}