Java date format时间格式化操作示例
本文实例讲述了Javadateformat时间格式化操作。分享给大家供大家参考,具体如下:
importjava.util.Date;
importjava.text.DateFormat;
/**
*格式化时间类
*DateFormat.FULL=0
*DateFormat.DEFAULT=2
*DateFormat.LONG=1
*DateFormat.MEDIUM=2
*DateFormat.SHORT=3
*@authorMichael
*@version1.0
*/
publicclassTest{
publicstaticvoidmain(String[]args){
Dated=newDate();
Strings;
/**Date类的格式:SatApr1613:17:29CST2016*/
System.out.println(d);
System.out.println("******************************************");
/**getDateInstance()*/
/**输出格式:2016-4-16*/
s=DateFormat.getDateInstance().format(d);
System.out.println(s);
/**输出格式:2016-4-16*/
s=DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);
/**输出格式:2016年4月16日星期六*/
s=DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);
/**输出格式:2016-4-16*/
s=DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);
/**输出格式:06-4-16*/
s=DateFormat.getDateInstance(DateFormat.sHORT).format(d);
System.out.println(s);
/**输出格式:2016-01-0100:00:00*/
java.text.DateFormatformat1=newjava.text.SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
s=format1.format(newDate());
System.out.println(s);
/**输出格式:2016-01-0101:00:00*/
System.out.println((newjava.text.SimpleDateFormat("yyyy-MM-ddhh:mm:ss")).format(newDate()));
/**输出格式:2016-01-0113:00:00*/
System.out.println((newjava.text.SimpleDateFormat("yyyy-MM-ddHH:mm:ss")).format(newDate()));
/**输出格式:20160101000000***/
java.text.DateFormatformat2=newjava.text.SimpleDateFormat("yyyyMMddhhmmss");
s=format2.format(newDate());
System.out.println(s);
}
}
补充一下:
Calendarcal=Calendar.getInstance();
cal.setTime(newDate());
System.out.println("SystemDate:"+cal.get(Calendar.MONTH+1));
注意下,月份是从0开始的,要Calendar.MONTH+1才可以的。
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc
在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
更多java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《Java图片操作技巧汇总》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》及《Java数据结构与算法教程》。
希望本文所述对大家java程序设计有所帮助。
热门推荐
10 生日牌祝福语英语简短
11 中考祝福语简短卡片大全
12 生日果酒祝福语简短独特
13 女儿结婚账单祝福语简短
14 形容新年的祝福语简短
15 职场祝福语大全 简短精辟
16 宝宝周岁可乐祝福语简短
17 送去考试祝福语大全简短
18 青春告白祖国简短祝福语