Java 8 获取当前日期
原创转载请注明出处: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
