Java 程式將日期格式化為 2019 年 4 月 19 日,下午 1:27
要設定格式並顯示日期時間,你需要使用 DateTimeFormatter。格式風格為 MEDIUM 和 SHORT
DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
顯示格式化的日期
formatter.format(LocalDateTime.now()
示例
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class Demo {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
System.out.println("Formatted Date = "+formatter.format(LocalDateTime.now()));
}
}輸出
Formatted Date = Apr 19, 2019, 1:27 PM
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP