//下面用simpleDateFormate和calendar类两种方式获取生日毫秒值结果差了很多啊????!!!!
package cn.itcast2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//算算你来这个世界多少天了????
public class Test1 {
public static void main(String[] args) throws ParseException {
// 获取当前时间毫秒值
Date now = new Date();
long nowTime = now.getTime();
System.out.println(nowTime);
// 获取生日的毫秒值-----------方式一 //9363天????
/* String birthday = "1990年07月09日";
SimpleDateFormat dfs = new SimpleDateFormat("YY年MM月dd日");
//将字符串转成日期对象
Date parse =dfs.parse(birthday);
//获取生日那天的毫秒值
long birthdayTime = parse.getTime();
System.out.println(birthdayTime);*/
// 获取生日的毫秒值-----------方式二 //9161天????
Calendar instance = Calendar.getInstance();
instance.set(1990, 7, 9);
long birthdayTime = instance.getTime().getTime();
// 做差
long time = nowTime - birthdayTime;
// 换算成天数
System.out.println(time / 1000 / 60 / 60 / 24);
}
}
|
|