取得当前日期,并按照不同日期格式化输入。代码如下:
1 // 20160618 SimpleDateFomat类的使用 日期格式化 练习
2 package MyPackage; //自己定义的包
3
4 import java.text.SimpleDateFormat; //导入需要的SimpleDateFormat包
5 import java.util.Date; //导入需要的Date包
6
7 class MyDateDemo { // 定义的MyDateDemo类
8 private SimpleDateFormat sd = null; // 声明SimpleDateFormat对象sd
9
10 public String getDate01() { // 定义getDate01方法
11 this.sd = new SimpleDateFormat("yyyy-MM-dd HH:mm;ss.sss"); // 得到一个"yyyy-MM-dd
12 // HH:mm;ss.sss"格式日期
13 return this.sd.format(new Date()); // 将当前日期进行格式化操作
14 }
15
16 public String getDate02() { // 定义getDate02方法
17 this.sd = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒sss毫秒"); // 得到一个"yyyy年MM月dd日
18 // HH时mm分ss秒sss毫秒"格式日期
19 return this.sd.format(new Date()); // 将当前日期进行格式化操作
20 }
21
22 public String getDate03() {// 定义getDate03方法
23 this.sd = new SimpleDateFormat("yyyyMMddHHmmsssss");// 得到一个"yyyyMMddHHmmsssss"格式日期(也就是时间戳)
24 return this.sd.format(new Date());// 将当前日期进行格式化操作
25 }
26 }
27
28 public class SimpleDateFormatDemo {// 主类
29
30 public static void main(String[] args) { // 主方法
31 MyDateDemo dd = new MyDateDemo(); // 声明dd对象,并实例化
32 System.out.println("默认日期格式: " + new Date());// 分别调用方法输入不同格式的日期
33 System.out.println("英文日期格式: " + dd.getDate01());
34 System.out.println("中文日期格式: " + dd.getDate02());
35 System.out.println("时间戳: " + dd.getDate03());
36
37 }
38
39 }
还有另一个日期格式化的方法,这种方法优先使用,另一种方法代码如下:
1 import java.util.* ; // 导入需要的工具包
2 class DateTime{ // 以后直接通过此类就可以取得日期时间
3 private Calendar calendar = null ; // 声明一个Calendar对象,取得时间
4 public DateTime(){ // 构造方法中直接实例化对象
5 this.calendar = new GregorianCalendar() ;
6 }
7 public String getDate(){ // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
8 // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
9 StringBuffer buf = new StringBuffer() ;
10 buf.append(calendar.get(Calendar.YEAR)).append("-") ; // 增加年
11 buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("-") ; // 增加月
12 buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append(" ") ; // 取得日
13 buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":") ; // 取得时
14 buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append(":") ;
15 buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append(".") ;
16 buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ;
17 return buf.toString() ;
18 }
19 public String getDateComplete(){ // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
20 // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
21 StringBuffer buf = new StringBuffer() ;
22 buf.append(calendar.get(Calendar.YEAR)).append("年") ; // 增加年
23 buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("月") ; // 增加月
24 buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append("日") ; // 取得日
25 buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append("时") ; // 取得时
26 buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append("分") ; // 取得分
27 buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append("秒") ; // 取得秒
28 buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)).append("毫秒") ; // 取得毫秒
29 return buf.toString() ;
30 }
31 public String getTimeStamp(){ // 得到的是一个时间戳
32 // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
33 StringBuffer buf = new StringBuffer() ;
34 buf.append(calendar.get(Calendar.YEAR)) ; // 增加年
35 buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)) ; // 增加月
36 buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)) ; // 取得日
37 buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)) ; // 取得时
38 buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)) ; // 取得分
39 buf.append(this.addZero(calendar.get(Calendar.SECOND),2)); // 取得秒
40 buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ; // 取得毫秒
41 return buf.toString() ;
42 }
43 // 考虑到日期中存在前导0,所以在此处加上补零的方法
44 private String addZero(int num,int len){
45 StringBuffer s = new StringBuffer() ;
46 s.append(num) ;
47 while(s.length()<len){ // 如果长度不足,则继续补0
48 s.insert(0,"0") ; // 在第一个位置处补0
49 }
50 return s.toString() ;
51 }
52 };
53 public class DateDemo06{
54 public static void main(String args[]){
55 DateTime dt = new DateTime() ;
56 System.out.println("系统日期:"+dt.getDate()) ;
57 System.out.println("中文日期:"+dt.getDateComplete()) ;
58 System.out.println("时间戳:"+dt.getTimeStamp()) ;
59 }
60 };
经常使用,就可以熟练记住和使用。