如何在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
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短