如何在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 红包祝福语简短10字
18 周六早晨祝福语简短