Java 中的 LocalDate format() 方法
在 Java 中,可以使用 LocalDate 类中的 format() 方法来使用指定的格式对 LocalDate 进行格式化。此方法需要一个参数,即要格式化的 LocalDate 对象,并且使用指定格式返回格式化的 LocalDate。
下面给出演示此方法的程序 −
示例
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate ld = LocalDate.parse("2019-02-14");
System.out.println("The LocalDate is: " + ld);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/YYYY");
System.out.println("The formatted LocalDate is: " + dtf.format(ld));
}
}输出
The LocalDate is: 2019-02-14 The formatted LocalDate is: 14/02/2019
现在,让我们了解一下上述程序。
首先显示 LocalDate。然后使用 format() 方法使用指定的格式对 LocalDate 进行格式化并显示格式化的 LocalDate。演示这一过程的代码片段如下 −
LocalDate ld = LocalDate.parse("2019-02-14");
System.out.println("The LocalDate is: " + ld);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/YYYY");
System.out.println("The formatted LocalDate is: " + dtf.format(ld));
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP