计算两个年龄差值的方法
public class Practice {
public static void main(String[] args) throws ParseException {
reckonAge(“2015-08-18”);
}
public static void reckonAge(String birthday1,String birthday2) throws ParseException {
DateFormat df = new SimpleDateFormat(“yyyy-MM-dd”);
//将两个日期转换成毫秒进行计算
long firstDate = df.parse(birthday1).getTime();
long secondDate = df.parse(birthday2).getTime();
//计算
long result = Math.abs(firstDate-secondDate
);
int age = (int) (result/1000/60/60/24/30);
int yu = age%12;
int zheng = age/12;
String newAge = zheng+“岁”+yu+“月”;
System.out.println(newAge);
}
}
博客展示了用Java计算两个年龄差值的方法。通过将日期转换为毫秒进行计算,得出两个日期的差值,再将差值转换为岁和月的形式输出。代码中定义了reckonAge方法,接收两个日期字符串作为参数。
1006

被折叠的 条评论
为什么被折叠?



