/**
* 将传入的Date时间转为字符串日期时间,转换格式(yyyy-MM-dd HH:mm:ss)
*/
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
事例:
public class Test2 {
public static void main(String[] args) {
Date date = new Date();
System.out.println("时间为: " + DateUtils.formatDateTime(date));
System.out.println("当前的时间为(不带年月日): " + DateUtils.getTime());
System.out.println("当前的时间为(带年月日): " + DateUtils.getDateTime());
}
}
控制台:
![](https://img.haomeiwen.com/i14992118/e4fa6cd713d11f15.png)
网友评论