提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用DateTimeFormatter格式化日期和时间
import java.time.DateTimeException; /** 来 自 n o w j a v a . c o m - 时 代 Java **/ import java.time.LocalDateTime; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { try { DateTimeFormatter dateFormatter = DateTimeFormatter .ofPattern("MMMM dd yyyy"); LocalDateTime now = LocalDateTime.now(); String output = now.format(dateFormatter); System.out.println(output); DateTimeFormatter dateFormatter2 = DateTimeFormatter .ofPattern("MM/dd/YY HH:mm:ss"); String output2 = now.format(dateFormatter2);//时代Java - nowjava.com 提供 System.out.println(output2); DateTimeFormatter dateFormatter3 = DateTimeFormatter .ofPattern("hh 'o''clock' a, zzzz"); ZonedDateTime zdt = ZonedDateTime.now(); String output