LocalTime 格式化() 方法


在 Java 的 LocalTime 類中,可以使用 format() 方法採用指定的格式設定器對 LocalTime 進行格式化。此方法需要一個引數,即將格式化的 LocalTime 物件,並返回經過指定的格式設定器格式化的 LocalTime。

用於演示的程式如下

示例

線上演示

import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class Main {
   public static void main(String[] args) {
      LocalTime lt = LocalTime.parse("14:30:47");
      System.out.println("The LocalTime is: " + lt);
      DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME;
      System.out.println("The formatted LocalTime is: " + dtf.format(lt));
   }
}

輸出

The LocalTime is: 14:30:47
The formatted LocalTime is: 14:30:47

現在我們來了解一下上面的程式。

首先,顯示 LocalTime。然後使用 format() 方法結合指定的格式設定器對 LocalTime 進行格式化並顯示格式化的 LocalTime。用程式碼片段示例如下

LocalTime lt = LocalTime.parse("14:30:47");
System.out.println("The LocalTime is: " + lt);
DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME;
System.out.println("The formatted LocalTime is: " + dtf.format(lt));

更新於:2019 年 7 月 30 日

115 次瀏覽

開啟你的 職業生涯

完成課程並取得認證

開始
廣告
© . All rights reserved.