java.time.LocalDateTime.format() 方法示例



說明

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

宣告

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

public String format(DateTimeFormatter formatter)

引數

formatter − 要使用的格式化程式,不能為空。

返回值

格式化後的日期字串,不能為空。

異常

DateTimeException − 如果在列印過程中發生錯誤。

示例

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

package com.tutorialspoint;

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
廣告
© . All rights reserved.