java.time.LocalDateTime.format(DateTimeFormatter formatter)方法

java.time.LocalDateTime.format(DateTimeFormatter formatter)方法使用指定的格式化程式格式化此日期時間。

聲明

以下是java.time.LocalDateTime.format(DateTimeFormatter formatter)方法的聲明。

public String format(DateTimeFormatter formatter)

參數

  • formatter - 要使用的格式化程式,而不是null

返回值

格式化的日期字串,不為null

異常

DateTimeException - 如果在列印期間發生錯誤。

示例

以下示例顯示了java.time.LocalDateTime.format(DateTimeFormatter formatter)方法的用法。

package com.zaixian;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeDemo {
   public static void main(String[] args) {

      LocalDateTime date = LocalDateTime.parse("2017-02-03T12:30:30");
      System.out.println(date);
      DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME;
      System.out.println(formatter.format(date));
   }
}

編譯並運行上面的程式,這將產生以下結果 -

2017-02-03T12:30:30
12:30:30

上一篇: java.time.LocalDateTime類 下一篇: java.time.LocalTime類