SimpleDateFormat日历格式化
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormat日历格式化 {
/**
* SimpleDateFormat日历格式化
* SimpleDateFormat 类可以对 Date 及字符串进行分析,并在它们之间相互转换,
* 它允许格式化(date -> text)、语法分析(text -> date)和标准化。它们的继承关系如下:
* java.lang.Object
* +----java.text.Format
* +----java.text.DateFormat
* +----java.text.SimpleDateFormat
*
* SimpleDateFormat 对日期和时间进行格式化
*
*/
/* 长日期格式*/
public static String toLongDateString(Date dt) {
SimpleDateFormat myFmt = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");
return myFmt.format(dt);
}
/* 短日期格式*/
public static String toShortDateString(Date dt) {
SimpleDateFormat myFmt = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
return myFmt.format(dt);
}
/* 长时间格式*/
public static String toLongTimeString(Date dt) {
SimpleDateFormat myFmt = new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
/* 短时间格式*/
public static String toShortTimeString(Date dt) {
SimpleDateFormat myFmt = new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}
public static void main(String[] args) {
Date now = new Date();
System.out.println("长日期格式:" + toLongDateString(now));
System.out.println("短日期格式:" + toShortDateString(now));
System.out.println("长时间格式:" + toLongTimeString(now));
System.out.println("短时间格式:" + toShortTimeString(now));
}
}
运行截图
![SimpleDateFormat日历格式化.jpg SimpleDateFormat日历格式化.jpg]()