提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用模式格式化日期和时间
import java.time.LocalDate; /*来自 nowjava - 时 代 Java*/ import java.time.LocalTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; import java.util.Locale; public class Main { public static void main(String[] args) { LocalDate ld = LocalDate.of(2019, Month.APRIL, 30); System.out.println("Formatting date:" + ld); format(ld, "M/d/yyyy"); format(ld, "MM/dd/yyyy"); format(ld, "MMM dd, yyyy"); format(ld, "MMMM dd, yyyy"); format(ld, "EEEE, MMMM dd, yyyy"); format(ld, "'Month' q 'in' QQQ"); format(ld, "[MM-dd-yyyy][' at' HH:mm:ss]"); /*时代Java - nowjava.com 提供*/ LocalTime lt = LocalTime.of(16, 30, 5, 78899); System.out.println("\nFormatting time:" + lt); format(lt, "HH:mm:ss"); format(lt, "KK:mm:ss a"); format(lt, "[MM-dd-yyyy][' at' HH:mm:ss]"); ZoneId usCentral = ZoneId.of("America/Chicago"); ZonedDateTime zdt = ZonedDateTime.of(ld, lt, usCentral); System.out.println("\nFormatting zoned datetime:" + zdt); format(zdt, "MM/dd/yyyy HH:mm:ssXXX"); format(zdt, "MM/dd/yyyy VV"); format(zdt, "[MM-dd-yyyy][' at' HH:mm:ss]"