获取时间戳
System.out.println(System.currentTimeMillis());
执行结果:1612613001108
==================================================================
输出时间
Date date = new Date();
System.out.println(date);
System.out.println(String.format("%tc",date));
System.out.println(String.format("%tF",date));
System.out.println(String.format("%tT",date));
执行结果:
Sat Feb 06 20:05:53 CST 2021
星期六 二月 06 20:05:53 CST 2021
2021-02-06
20:05:53
==================================================================
格式化时间
Date date = new Date();
DateFormat dateformat2= new SimpleDateFormat("yyyy-MM-dd-//-HH-mm-ss");
String date2 = dateformat2.format(date);
System.out.println(date2);
DateFormat dateformat3= new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String date3 = dateformat3.format(date);
System.out.println(date3);
执行结果:
2021-02-06-//-20-09-42
2021-02-06-20-09-42
=========================================================================
Date date = new Date();
String nianyueri = String.format("%tF",date);
String shi = String.format("%tH",date);
String fen = String.format("%tM",date);
String miao = String.format("%tS",date);
String haomiao = String.format("%tL",date);
System.out.println(nianyueri+"-"+shi+":"+fen+":"+miao+":"+haomiao);
System.out.println(nianyueri+"+"+shi+"-"+fen+"+"+miao+"-"+haomiao);
执行结果:
2021-02-06-20:28:04:352
2021-02-06+20-28+04-352
=========================================================================