要格式化和显示日期(例如日/月/年),您需要设置模式:
dd/MM/yyyy
首先,设置一个LocalDate:
LocalDate localDate = LocalDate.now();
现在格式化并显示日期为'12 / 04/2019':
localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println("Date = "+date); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); System.out.println("Formatted Date = "+date.format(formatter)); } }
输出结果
Date = 2019-04-12 Formatted Date = 12/04/2019