要格式化和显示日期时间,您需要使用DateTimeFormatter。格式样式为MEDIUM和SHORT:
DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
显示格式化日期:
formatter.format(LocalDateTime.now()
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; public class Demo { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT); System.out.println("Formatted Date = "+formatter.format(LocalDateTime.now())); } }
输出结果
Formatted Date = Apr 19, 2019, 1:27 PM