Java教程

JAVA 获取当前时间(年月日时分秒)

本文主要是介绍JAVA 获取当前时间(年月日时分秒),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

获取当前时间(年月日时分秒)

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
这篇关于JAVA 获取当前时间(年月日时分秒)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!