Пример #1
0
  public static void main(String[] args) {
    ZonedDateTime apollo11launch =
        ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("America/New_York"));

    String formatted = DateTimeFormatter.ISO_DATE_TIME.format(apollo11launch);
    // 1969-07-16T09:32:00-05:00[America/New_York]
    System.out.println(formatted);

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
    formatted = formatter.format(apollo11launch);
    // July 16, 1969 9:32:00 AM EDT
    System.out.println(formatted);
    formatted = formatter.withLocale(Locale.FRENCH).format(apollo11launch);
    // 16 juillet 1969 09:32:00 EDT
    System.out.println(formatted);

    formatter = DateTimeFormatter.ofPattern("E yyyy-MM-dd HH:mm");
    formatted = formatter.format(apollo11launch);
    System.out.println(formatted);

    LocalDate churchsBirthday = LocalDate.parse("1903-06-14");
    System.out.println("churchsBirthday: " + churchsBirthday);
    apollo11launch =
        ZonedDateTime.parse(
            "1969-07-16 03:32:00-0400", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssxx"));
    System.out.println("apollo11launch: " + apollo11launch);
  }
Пример #2
0
 private void book() {
   // jdk8 新增格式器  TimeDateFormatter()
   DateTimeFormatter[] formatters =
       new DateTimeFormatter[] {
         // 使用常量创建DateTimeformatter格式器
         DateTimeFormatter.ISO_LOCAL_DATE,
         DateTimeFormatter.ISO_LOCAL_TIME,
         DateTimeFormatter.ISO_DATE_TIME,
         // 使用本地化的不同风格创建DateTimeformatter格式器
         DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.LONG),
         DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG),
         // 根据模式字符串创建DateTimeformatter格式器
         DateTimeFormatter.ofPattern("G yyyy MMM dd HH:mm:ss"),
       };
   LocalDateTime date = LocalDateTime.now();
   for (int i = 0; i < formatters.length; i++) {
     System.out.println(formatters[i].format(date));
   }
 }
/** @author maxrenet */
public class DateExample {

  static LocalDate one = LocalDate.now();
  static LocalDateTime two = LocalDateTime.now();
  static LocalTime three = LocalTime.now();
  static String dateOne = one.format(DateTimeFormatter.ISO_DATE);
  static String dateTwo = two.format(DateTimeFormatter.ISO_DATE);
  static String dateThree = two.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
  static String dateFour = two.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
  static String dateFive = two.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
  // static String timeThree=three.format(DateTimeFormatter.ISO_LOCAL_TIME);
  public static void main(String[] args) {
    System.out.println(two);
    System.out.println("ISODATE returns " + dateTwo);
    System.out.println("Short date returns " + dateThree);
    System.out.println("Medium date returns: " + dateFour);
    System.out.println("Long date returns: " + dateFive);
  }
}