提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用预定义的格式模式
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month;//来自 时 代 J a v a 公 众 号 - nowjava.com import java.time.format.DateTimeFormatter; import static java.time.format.FormatStyle.FULL; import static java.time.format.FormatStyle.LONG; import static java.time.format.FormatStyle.MEDIUM; import static java.time.format.FormatStyle.SHORT; import java.util.Locale; public class Main { public static void main(String[] args) { LocalDate ld = LocalDate.of(2012, Month.APRIL, 19); LocalTime lt = LocalTime.of(16, 30, 20); LocalDateTime ldt = LocalDateTime.of(ld, lt); DateTimeFormatter fmt = DateTimeFormatter.ofLocalizedDate(SHORT); /** from * N o w J a v a . c o m - 时 代 Java **/ System.out.println("Formatter Default Locale: " + fmt.getLocale()); System.out.println("Short Date: " + fmt.format(ld)); fmt = DateTimeFormatter.ofLocalizedDate(MEDIUM); System.out.println("Medium Date: " + fmt.format(ld)); fmt = DateTimeFormatter.ofLocalizedDate(LONG); System.out.println("Long Date: " + fmt.format(ld)); fmt = DateTimeFormatter.ofLocalizedDate(FULL); System.out.println("Full Date: " + fmt.format(ld)); fmt = DateTimeFormatter.ofLocalizedTime(SHORT); System.out.println("Short Time: " + fmt.format(lt)); fmt = DateTimeFormatter.ofLocalizedDateTime(SHORT); System.out.println("Short Datetime: " + fmt.format(ldt)); fmt = DateTimeFormatter.ofLocalizedDateTime(MEDIUM); System.out.println("Medium Datetime: " + fmt.format(ldt)); // Use German locale to format the datetime in medius style fmt = DateTimeFormatter.ofLocalizedDateTime(MEDIUM) .withLocale(Locale.GERMAN); System.out.println("German Medium Datetime: " + fmt.format(ldt)); // Use Indian(English) locale to format datetime in short style fmt = DateTimeFormatter.ofLocalizedDateTime(SHORT) .withLocale(new Locale("en",