如何在 Java 中将日期格式化为并显示为“201904”
要将日期格式化为并显示为“YearMonth”,你需要设置模式
uuuuMM
首先,设置一个 LocalDate
LocalDate localDate = LocalDate.now();
现在将日期格式化为“201904”并显示出来
localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))
示例
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("Date = "+localDate); System.out.println("Date (Year and Month) = "+localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))); } }
输出
Date = 2019-04-19 Date (Year and Month) = 201904
广告