ITeye博客

Java 8 获取当前日期

fairy_xzc · 2018-12-12 14:38 · Java · 编程语言 · Markdown

原创转载请注明出处:https://agilestyle.iteye.com/blog/2435106

 

Code Demo

public static String getCurrentDateTime() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return LocalDateTime.now().format(formatter);
}

public static String getCurrentDateTime(String format) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    return LocalDateTime.now().format(formatter);
}

 

Test

System.out.println(getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println(getCurrentDateTime("yyMMddHHmmss"));
System.out.println(getCurrentDateTime("yyyyMMdd"));
System.out.println(getCurrentDateTime("HHmmss"));
System.out.println(getCurrentDateTime());

 

Console Output

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。