获取当前时间(年月日时分秒)
Date d =
new
Date();
SimpleDateFormat sbf =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
System.out.println(sbf.format(d));
或
System.out.println(
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
).format(
new
Date()));
2020
-
04
-
28
14
:
23
:
05
获取当前时间戳 到毫秒
System.out.println(System.currentTimeMillis());
1588055609783
时间戳换成年月日时间时分秒
Date date =
new
Date(System.currentTimeMillis());
DateFormat dateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss"
);
String format = dateFormat.format(date);
System.out.println(format);
1588055609783
-->
2020
-
04
-
28
02
:
33
:
29
年月日时间时分秒换成时间戳
leDateFormat simpleDateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
Date date = simpleDateFormat.parse(
"2020-04-28 02:33:29"
);
long
ts = date.getTime();
System.out.println(ts);
String res = String.valueOf(ts);
// 转化为字符串
System.out.println(res);
1588012409000
内容转载来自:
https://www.cnblogs.com/yoyo1216/p/12794252.html