1.获取当前时间
Date nowTime = new Date();
2.将Date转化为一定格式的String
格式化字符表:
具体格式对照表:
//年-月-日 时:分:秒 毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String timeStr = sdf.format(new Date());
输出结果:年-月-日 时:分:秒 毫秒
3.日期字符串转化为Date对象
//注意:SimpleDateFormat构造方法参数的日期格式必须与待转化字符串的日期格式一致
String time = "2022-05-22 09:42:36 888";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date Time = sdf.parse(time);
输出结果Time:
4.System.currentTimeMillis()
(1)获取到的是当前时间距1970-01-01 00:00:00 000的毫秒数
Long nowTimeMillis = System.currentTimeMillis();