Date nowTime = new Date();
System.out.println(nowTime);// Sun Mar 06 17:17:37 CST 2022
Date nowTime2 = new Date(1000);
System.out.println(nowTime2);// Thu Jan 01 08:00:01 CST 1970
格式化输出Date类型数据
SimpleDateFormat类
1、详细使用具体看String类型日期和Date类型日期之间的互转
String类型日期转换为Date类型
String s = "2022年03月06日 05:23:44:802";
SimpleDateFormat now = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss:SSS");
Date nT = now.parse(s);
System.out.println(nT);
Date类型转换为String类型
SimpleDateFormat nowTimeFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss:SSS");
String nowTimeStr = nowTimeFormat.format(nowTime);
System.out.println(nowTimeStr);
统计某条语句(方法)执行所耗费的时长
long start = System.currentTimeMillis();// 方法执行前记录一个毫秒数
print();
long end = System.currentTimeMillis();// 方法执行后记录一个毫秒数
System.out.println("耗时:" + (end - start) + "毫秒");
// 两个毫秒数相减,得到print()方法耗费时长
获取昨天此时的时间
// 获取昨天此时的时间
Date yTime = new Date(System.currentTimeMillis() - 60L * 60L * 24L * 60L * 1000L);
System.out.println(yTime);// Wed Jan 05 18:31:01 CST 2022



