SimpleDateFormat使用详解——日期、字符串应用

public class SimpleDateFormat extends DateFormat

SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。

SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的方式启动。 但是,希望用 DateFormat 中的getTimeInstance、 getDateInstance 或 getDateTimeInstance 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 applyPattern 方法修改格式化方式。

SimpleDateFormat函数的继承关系:
java.lang.Object
   |
   +----java.text.Format
           |
           +----java.text.DateFormat
                   |
                   +----java.text.SimpleDateFormat

下面是个小例子:

 1 import java.text.*;
 2 import java.util.Date;
 3 
 4 /**
 5   SimpleDateFormat函数语法:
 6   
 7   G 年代标志符
 8   y 年
 9   M 月
10   d 日
11   h 时 在上午或下午 (1~12)
12   H 时 在一天中 (0~23)
13   m 分
14   s 秒
15   S 毫秒
16   E 星期
17   D 一年中的第几天
18   F 一月中第几个星期几
19   w 一年中第几个星期
20   W 一月中第几个星期
21   a 上午 / 下午 标记符 
22   k 时 在一天中 (1~24)
23   K 时 在上午或下午 (0~11)
24   z 时区
25  */
26 public class FormatDateTime {
27 
28     public static void main(String[] args) {
29         SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
30         SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); 
31         SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
32         SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
33         SimpleDateFormat myFmt4=new SimpleDateFormat(
34                 "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
35         Date now=new Date();
36         System.out.println(myFmt.format(now));
37         System.out.println(myFmt1.format(now));
38         System.out.println(myFmt2.format(now));
39         System.out.println(myFmt3.format(now));
40         System.out.println(myFmt4.format(now));
41         System.out.println(now.toGMTString());
42         System.out.println(now.toLocaleString());
43         System.out.println(now.toString());
44     }    
45     
46 }


效果:
2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四 
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004

下面是个JavaBean:

 1 public class FormatDateTime {
 2     
 3     public static String toLongDateString(Date dt){
 4         SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");        
 5         return myFmt.format(dt);
 6     }
 7     
 8     public static String toShortDateString(Date dt){
 9         SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分");        
10         return myFmt.format(dt);
11     }    
12     
13     public static String toLongTimeString(Date dt){
14         SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");        
15         return myFmt.format(dt);
16     }
17     public static String toShortTimeString(Date dt){
18         SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");        
19         return myFmt.format(dt);
20     }
21     
22     public static void main(String[] args) {
23 
24         Date now=new Date();
25 
26         System.out.println(FormatDateTime.toLongDateString(now));
27         System.out.println(FormatDateTime.toShortDateString(now));
28         System.out.println(FormatDateTime.toLongTimeString(now));
29         System.out.println(FormatDateTime.toShortTimeString(now));
30     }    
31     
32 }

调用的main 测试结果:
2004年12月16日 17时38分26秒 星期四 
04年12月16日 17时38分
17 38 26 0965
04/12/16 17:38

计算两个日期差

 1 import java.text.DateFormat;
 2 import java.text.ParseException;
 3 import java.text.SimpleDateFormat;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 /******
 7  * 
 8  * @author xiongcb
 9  * 实现计算两个日期的月份差
10  * 年份加一年
11  * 比较两个日期大小
12  * 按规定格式输出日期
13  */
14 
15 public class Riqi {
16 
17  
18 public static void main(String[] args) throws ParseException {
19  String s="20120601";
20  String s1="20110512";
21  Date m=new Date();
22  Date d = null;
23  Date d1 = null;
24  
25  
26  
27     DateFormat df=new SimpleDateFormat("yyyyMMdd");
28     try {  
29      d = df.parse(s);
30      d1=df.parse(s1);
31      
32      //比较日期大小
33      
34      if(d.getTime()>d1.getTime())
35        {
36        System.out.println(df.format(d));
37         }
38       else
39       {
40        System.out.println(df.format(d1));
41       }
42      } 
43     catch (ParseException e) 
44     {    
45      e.printStackTrace(); }
46  //   Date date=df.parse(s);
47  //   System.out.println(d);
48  //   System.out.println(df.format(d));
49     Calendar c = Calendar.getInstance();
50     
51     c.setTime(d);
52     c.add(Calendar.YEAR, 1);  //年份加一年
53     System.out.println(df.format(c.getTime()));//按照yyyyMMdd格式输出
54   
55     
56     int year = c.get(Calendar.YEAR);
57     int month = c.get(Calendar.MONTH);
58     
59     c.setTime(d1);
60     int year1 = c.get(Calendar.YEAR);
61     int month1 = c.get(Calendar.MONTH);
62     
63     int result;
64     if(year==year1)
65     {
66      
67      result=month1-month;//两个日期相差几个月,即月份差
68     }
69     else
70     {
71      result=12*(year1-year)+month1-month;//两个日期相差几个月,即月份差
72     }
73     
74 
75    
76    
77 }
78 }

 字符串转换为日期

1 DateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
2 Datea d = f.parse("2005-11-07 11:22:55");
原文地址:https://www.cnblogs.com/zl1991/p/6253428.html