天天看点

LocalDateTime日期格式化/LocalDateTime转String

LocalDateTime格式化代码

public static void main(String[] args){
   	try {
           LocalDateTime localDateTime = LocalDateTime.now();
           String str1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDateTime);
           String str2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒").format(localDateTime);
           String str3 = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(localDateTime);
           String str4 = DateTimeFormatter.ofPattern("yyyy年MM月dd日").format(localDateTime);
           System.out.println("str1--->"+str1);
           System.out.println("str2--->"+str2);
           System.out.println("str3--->"+str3);
           System.out.println("str4--->"+str4);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
           

运行结果

str1--->2021-01-06 11:43:26
str2--->2021年01月06日 11时43分26秒
str3--->2021-01-06
str4--->2021年01月06日