获取系统当前时间
-
Date
Date date = new Date();
System.out.println(date);
运行结果:
Fri May 08 15:59:38 CST 2020
-
SimpleDateFormat
public static String getTime(String format){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
String nowTime = simpleDateFormat .format(new Date());
return nowTime ;
}
public static void main(String[] args){
String timeNow = getTime("yyyyMMddHHmmss");
System.out.println(timeNow);
timeNow = getTime("yyyy-MM-dd-HH:mm:ss");
System.out.println(timeNow);
}
运行结果:
20200508160436
2020-05-08-16:10:15