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



說明

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

宣告

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

public String format(DateTimeFormatter formatter)

引數

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

返回值

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

異常

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

示例

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

package com.tutorialspoint;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

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

      LocalDate date = LocalDate.parse("2017-02-03");
      System.out.println(date);  
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
      System.out.println(formatter.format(date));  
   }
}

讓我們編譯並執行上述程式,它將產生以下結果 −

2017-02-03
03/02/2017
廣告