s-day01--DateFormat_Date对象转成指定格式的字符串
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.inmind.date02;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/*
|
||||
Date展示的时间,使用歪果仁喜欢的时间,中国人不喜欢,能不能变成熟悉的年月日时分秒??
|
||||
可以,使用java中DateFormat类,但是它是抽象类,我们必须使用它的子类SimpleDateFormat
|
||||
|
||||
构造方法:
|
||||
SimpleDateFormat(String pattern) 使用给定模式构建一个 SimpleDateFormat ,默认日期格式符号为默认的 FORMAT区域设置。
|
||||
|
||||
字母模式: y 表示年 M表示月 d表示日 H表示时 m表示分 s表示秒 S表示毫秒
|
||||
|
||||
希望展示的时间:2025年07月25日 11时11分11秒
|
||||
对应的模式:yyyy年MM月dd日 HH时mm分ss秒
|
||||
|
||||
常用方法:
|
||||
String format(Date date) 将日期对象格式化成日期/时间字符串。
|
||||
|
||||
*/
|
||||
public class Demo06 {
|
||||
public static void main(String[] args) {
|
||||
//我需要一个时间
|
||||
Date date = new Date();
|
||||
System.out.println(date);//Sat Oct 10 14:16:59 CST 2026
|
||||
//我创建一个日期格式化对象,将时间按照2025年07月25日 11时11分11秒展示
|
||||
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String dateStr = sdf.format(date);
|
||||
System.out.println(dateStr);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user