DateFormat类的format方法
-
使用DateFormat类中的format方法,把日期格式转化为文本
-
使用步骤
1、创建SimpleDateFormat对象,构造方法中传递指定的模式
2、调用SimpleDateFormat对象中的方法format,按照构造方法中指定的模式,把Date 日期转化为符合模式的字符串(文本)
package com.company;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Date1 {
public static void main(String[] args) {
//创建SimpleDateFormat对象,构造方法中传递指定的模式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 HH时mm分ss秒");
//调用SimpleDateFormat对象中的方法format,按照构造方法中指定的模式,把Date日期转化为符合模式的字符串(文本)
Date date = new Date();
String d = sdf.format(date);
System.out.println(date);//Wed Feb 26 18:29:04 CST 2020
System.out.println(d);//2020年29月26日 18时29分04秒
}
}