将Date格式的数据按照规定格式输出
使用 SimpleDateFormat 类格式化当前日期并打印,日期格式为“xxxx 年 xx 月 xx 日星期 xxx 点 xx 分 xx 秒”
public static void main(String[] args) {
Date now = new Date(); // 创建一个Date对象,获取当前时间
// 指定格式化格式
SimpleDateFormat f = new SimpleDateFormat("今天是 " + "yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒");
System.out.println(f.format(now)); // 将当前时间袼式化为指定的格式
}
输出
今天是 2018 年 10 月 15 日 星期一 09 点 26 分 23 秒
实际使用
数据是从Map取到的Object格式,需要转为特定的格式输出:
Date date=(Date) eInfo.get("2");
// 指定格式化格式
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(f.format(date)); // 将当前时间袼式化为指定的格式
map.put("date",f.format(date));
完成~