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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM