如何在 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP