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



說明

java.time.OffsetDateTime.format(DateTimeFormatter formatter) 方法使用指定的格式化程式對這個日期時間進行格式化。

宣告

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

public String format(DateTimeFormatter formatter)

引數

formatter - 要使用的格式化程式,不可為 null。

返回值

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

異常

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

示例

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

package com.tutorialspoint;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

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

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

讓我們編譯並執行以上程式,會生成以下結果 -

2017-02-03T12:30:30+01:00
12:30:30+01:00
廣告
© . All rights reserved.