skywang12345

导航

 

 

本章主要介绍DateFormat。

 


DateFormat 介绍

DateFormat 的作用是 格式化并解析“日期/时间”。实际上,它是Date的格式化工具,它能帮助我们格式化Date,进而将Date转换成我们想要的String字符串供我们使用
不过DateFormat的格式化Date的功能有限,没有SimpleDateFormat强大;但DateFormat是SimpleDateFormat的父类。所以,我们先对DateFormat有个整体了解,然后再学习SimpleDateFormat。

 

DateFormat 的作用是格式化Date。它支持格式化风格包括 FULLLONGMEDIUMSHORT 共4种:

(01) DateFormat.SHORT
        完全为数字,如 12.13.52 或 3:30pm
(02) DateFormat.MEDIUM
        较长,如 Jan 12, 1952
(03) DateFormat.LONG
        更长,如 January 12, 1952 或 3:30:32pm
(04) DateFormat.FULL
        是完全指定,如 Tuesday、April 12、1952 AD 或 3:30:42pm PST。

 

DateFormat 的定义如下

public abstract class NumberFormat extends Format {}

 

DateFormat 的函数接口

// 默认构造函数
DateFormat()

// 非构造函数
Object                   clone()
boolean                  equals(Object object)
abstract StringBuffer    format(Date date, StringBuffer buffer, FieldPosition field)
final StringBuffer       format(Object object, StringBuffer buffer, FieldPosition field)
final String             format(Date date)
static Locale[]          getAvailableLocales()
Calendar                 getCalendar()
final static DateFormat     getInstance()
final static DateFormat     getDateInstance()
final static DateFormat     getDateInstance(int style)
final static DateFormat     getDateInstance(int style, Locale locale)
final static DateFormat     getTimeInstance()
final static DateFormat     getTimeInstance(int style)
final static DateFormat     getTimeInstance(int style, Locale locale)
final static DateFormat     getDateTimeInstance()
final static DateFormat     getDateTimeInstance(int dateStyle, int timeStyle)
final static DateFormat     getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
NumberFormat     getNumberFormat()
TimeZone         getTimeZone()
int              hashCode()
boolean          isLenient()
Date             parse(String string)
abstract Date    parse(String string, ParsePosition position)
Object           parseObject(String string, ParsePosition position)
void             setCalendar(Calendar cal)
void             setLenient(boolean value)
void             setNumberFormat(NumberFormat format)
void             setTimeZone(TimeZone timezone)

 

注意:DateFormat是一个抽象类。

当我们通过DateFormat的 getInstance(), getDateInstance()和getDateTimeInstance() 获取DateFormat实例时;实际上是返回的SimpleDateFormat对象。
下面的函数实际上都是返回的SimpleDateFormat对象

final static DateFormat getInstance()
final static DateFormat getTimeInstance()
final static DateFormat getTimeInstance(int style)
final static DateFormat getTimeInstance(int style, Locale locale)
final static DateFormat getDateInstance()
final static DateFormat getDateInstance(int style)
final static DateFormat getDateInstance(int style, Locale locale)
final static DateFormat getDateTimeInstance()
final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)

这些函数在SimpleDateFormat.java中的定义如下:

  1 public static final int FULL = 0;
  2 public static final int LONG = 1;
  3 public static final int MEDIUM = 2;
  4 public static final int SHORT = 3;
  5 public static final int DEFAULT = MEDIUM;
  6 
  7 public final static DateFormat getInstance() {
  8     return getDateTimeInstance(SHORT, SHORT);
  9 }
 10 
 11 public final static DateFormat getTimeInstance()
 12 {
 13     return get(DEFAULT, 0, 1, Locale.getDefault());
 14 }
 15 
 16 public final static DateFormat getTimeInstance(int style)
 17 {
 18     return get(style, 0, 1, Locale.getDefault());
 19 }
 20 
 21 public final static DateFormat getTimeInstance(int style,
 22                                              Locale aLocale)
 23 {
 24     return get(style, 0, 1, aLocale);
 25 }
 26 
 27 public final static DateFormat getDateInstance()
 28 {
 29     return get(0, DEFAULT, 2, Locale.getDefault());
 30 }
 31 
 32 public final static DateFormat getDateInstance(int style)
 33 {
 34     return get(0, style, 2, Locale.getDefault());
 35 }
 36 
 37 public final static DateFormat getDateInstance(int style,
 38                                              Locale aLocale)
 39 {
 40     return get(0, style, 2, aLocale);
 41 }
 42 
 43 public final static DateFormat getDateTimeInstance()
 44 {
 45     return get(DEFAULT, DEFAULT, 3, Locale.getDefault());
 46 }
 47 
 48 public final static DateFormat getDateTimeInstance(int dateStyle,
 49                                                    int timeStyle)
 50 {
 51     return get(timeStyle, dateStyle, 3, Locale.getDefault());
 52 }
 53 
 54 public final static DateFormat
 55     getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
 56 {
 57     return get(timeStyle, dateStyle, 3, aLocale);
 58 }
 59 
 60 /**
 61  * 获取DateFormat实例,实际上是返回SimpleDateFormat对象。
 62  * 
 63  * timeStyle -- 值可以为“FULL”或“LONG”或“MEDIUM”或“SHORT”
 64  * dateStyle -- 值可以为“FULL”或“LONG”或“MEDIUM”或“SHORT”
 65  * flags     -- 值可以为“1”或“2”或“3”。
 66  *       1 表示获取“时间样式”
 67  *       2 表示获取“日期样式”
 68  *       3 表示获取“时间和日期样式”
 69  * loc       -- locale对象,表示“区域”
 70  */
 71 private static DateFormat get(int timeStyle, int dateStyle,
 72                               int flags, Locale loc) {
 73     if ((flags & 1) != 0) {
 74         if (timeStyle < 0 || timeStyle > 3) {
 75             throw new IllegalArgumentException("Illegal time style " + timeStyle);
 76         }
 77     } else {
 78         timeStyle = -1;
 79     }
 80     if ((flags & 2) != 0) {
 81         if (dateStyle < 0 || dateStyle > 3) {
 82             throw new IllegalArgumentException("Illegal date style " + dateStyle);
 83         }
 84     } else {
 85         dateStyle = -1;
 86     }
 87     try {
 88         // Check whether a provider can provide an implementation that's closer 
 89         // to the requested locale than what the Java runtime itself can provide.
 90         LocaleServiceProviderPool pool =
 91             LocaleServiceProviderPool.getPool(DateFormatProvider.class);
 92         if (pool.hasProviders()) {
 93             DateFormat providersInstance = pool.getLocalizedObject(
 94                                                 DateFormatGetter.INSTANCE,
 95                                                 loc, 
 96                                                 timeStyle,
 97                                                 dateStyle,
 98                                                 flags);
 99             if (providersInstance != null) {
100                 return providersInstance;
101             }
102         }
103 
104         return new SimpleDateFormat(timeStyle, dateStyle, loc);
105     } catch (MissingResourceException e) {
106         return new SimpleDateFormat("M/d/yy h:mm a");
107     }
108 }
View Code

通过上面的代码,我们能够进一步的认识到:DateFormat的作用是格式化Date;帮助我们将Date转换成我们需要的String字符串。DateFormat提供的功能非常有限,它只能支持FULL、LONG、MEDIUM 和 SHORT 这4种格式。而且,我们获取DateFormat实例时,实际上是返回的SimpleDateFormat对象。

 


DateFormat 实例

下面,我们通过实例学习使用DateFormat的常用API。
源码如下(DateFormatTest.java): 

  1 import java.util.Date;
  2 import java.util.Locale;
  3 import java.text.DateFormat;
  4 import java.text.FieldPosition;
  5 
  6 /**
  7  * DateFormat 的API测试程序
  8  *
  9  * @author skywang
 10  * @email kuiwu-wang@163.com
 11  */
 12 public class DateFormatTest {
 13     
 14     public static void main(String[] args) {
 15 
 16         // 只显示“时间”:调用getTimeInstance()函数
 17         testGetTimeInstance() ;
 18 
 19         // 只显示“日期”:调用getDateInstance()函数
 20         testGetDateInstance() ;
 21 
 22         // 显示“日期”+“时间”:调用getDateTimeInstance()函数
 23         testGetDateTimeInstance() ;
 24         
 25         // 测试format()函数
 26         testFormat();
 27     }
 28 
 29     /**
 30      * 测试DateFormat的getTimeInstance()函数
 31      * 它共有3种重载形式:
 32      * (01) getTimeInstance()
 33      * (02) getTimeInstance(int style)
 34      * (03) getTimeInstance(int style, Locale locale)
 35      *
 36      * @author skywang
 37      */
 38     private static void testGetTimeInstance() {
 39         Date date = new Date(); 
 40 
 41         //Locale locale = new Locale("fr", "FR");
 42         Locale locale = new Locale("zh", "CN"); 
 43 
 44         // 等价于 DateFormat.getTimeInstance( DateFormat.MEDIUM); 
 45         DateFormat short0  = DateFormat.getTimeInstance( ); 
 46 
 47         // 参数是:“时间的显示样式”
 48         DateFormat short1  = DateFormat.getTimeInstance( DateFormat.SHORT); 
 49         DateFormat medium1 = DateFormat.getTimeInstance( DateFormat.MEDIUM); 
 50         DateFormat long1   = DateFormat.getTimeInstance( DateFormat.LONG); 
 51         DateFormat full1   = DateFormat.getTimeInstance( DateFormat.FULL); 
 52 
 53         // 参数是:“时间的显示样式” 和 “地区”
 54         DateFormat short2  = DateFormat.getTimeInstance( DateFormat.SHORT, locale); 
 55         DateFormat medium2 = DateFormat.getTimeInstance( DateFormat.MEDIUM, locale); 
 56         DateFormat long2   = DateFormat.getTimeInstance( DateFormat.LONG, locale); 
 57         DateFormat full2   = DateFormat.getTimeInstance( DateFormat.FULL, locale); 
 58 
 59         System.out.println("\n----getTimeInstance ----\n"
 60                 + "(1.0) Empty Param   : " + short0.format(date) +"\n"
 61                 + "(2.1) One Param(s)  : " + short1.format(date) +"\n"
 62                 + "(2.2) One Param(m)  : " + medium1.format(date) +"\n"
 63                 + "(2.3) One Param(l)  : " + long1.format(date) +"\n"
 64                 + "(2.4) One Param(f)  : " + full1.format(date) +"\n"
 65                 + "(3.1) One Param(s,l): " + short2.format(date) +"\n"
 66                 + "(3.2) One Param(m,l): " + medium2.format(date) +"\n"
 67                 + "(3.3) One Param(l,l): " + long2.format(date) +"\n"
 68                 + "(3.4) One Param(f,l): " + full2.format(date) +"\n"
 69                 ); 
 70     }
 71 
 72     /**
 73      * 测试DateFormat的getDateTimeInstance()函数
 74      * 它共有3种重载形式:
 75      * (01) getDateInstance()
 76      * (02) getDateInstance(int style)
 77      * (03) getDateInstance(int style, Locale locale)
 78      */
 79     public static void testGetDateTimeInstance() {
 80         Date date = new Date(); 
 81 
 82         Locale locale = new Locale("zh", "CN"); 
 83 
 84         // 等价于 DateFormat.getDateTimeInstance( DateFormat.MEDIUM); 
 85         DateFormat short0  = DateFormat.getDateTimeInstance( ); 
 86 
 87         DateFormat short1  = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT); 
 88         DateFormat medium1 = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM); 
 89         DateFormat long1   = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG); 
 90         DateFormat full1   = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL); 
 91 
 92         DateFormat short2  = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, locale); 
 93         DateFormat medium2 = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale); 
 94         DateFormat long2   = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG, locale); 
 95         DateFormat full2   = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL, locale); 
 96 
 97         System.out.println("\n----getDateTimeInstance ----\n"
 98                 + "(1.0) Empty Param   : " + short0.format(date) +"\n"
 99                 + "(2.1) One Param(s)  : " + short1.format(date) +"\n"
100                 + "(2.2) One Param(m)  : " + medium1.format(date) +"\n"
101                 + "(2.3) One Param(l)  : " + long1.format(date) +"\n"
102                 + "(2.4) One Param(f)  : " + full1.format(date) +"\n"
103                 + "(3.1) One Param(s,l): " + short2.format(date) +"\n"
104                 + "(3.2) One Param(m,l): " + medium2.format(date) +"\n"
105                 + "(3.3) One Param(l,l): " + long2.format(date) +"\n"
106                 + "(3.4) One Param(f,l): " + full2.format(date) +"\n"
107                 ); 
108     }
109 
110     /**
111      * 测试DateFormat的getDateInstance()函数
112      * 它共有3种重载形式:
113      * (01) getDateTimeInstance()
114      * (02) getDateTimeInstance(int dateStyle, int timeStyle)
115      * (03) getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
116      */
117     public static void testGetDateInstance() {
118         Date date = new Date(); 
119 
120         //Locale locale = new Locale("en", "US"); 
121         Locale locale = new Locale("zh", "CN"); 
122 
123         // 等价于 DateFormat.getDateInstance( DateFormat.MEDIUM); 
124         DateFormat short0  = DateFormat.getDateInstance( ); 
125 
126         DateFormat short1  = DateFormat.getDateInstance( DateFormat.SHORT); 
127         DateFormat medium1 = DateFormat.getDateInstance( DateFormat.MEDIUM); 
128         DateFormat long1   = DateFormat.getDateInstance( DateFormat.LONG); 
129         DateFormat full1   = DateFormat.getDateInstance( DateFormat.FULL); 
130 
131         DateFormat short2  = DateFormat.getDateInstance( DateFormat.SHORT, locale); 
132         DateFormat medium2 = DateFormat.getDateInstance( DateFormat.MEDIUM, locale); 
133         DateFormat long2   = DateFormat.getDateInstance( DateFormat.LONG, locale); 
134         DateFormat full2   = DateFormat.getDateInstance( DateFormat.FULL, locale); 
135 
136         System.out.println("\n----getDateInstance ----\n"
137                 + "(1.0) Empty Param   : " + short0.format(date) +"\n"
138                 + "(2.1) One Param(s)  : " + short1.format(date) +"\n"
139                 + "(2.2) One Param(m)  : " + medium1.format(date) +"\n"
140                 + "(2.3) One Param(l)  : " + long1.format(date) +"\n"
141                 + "(2.4) One Param(f)  : " + full1.format(date) +"\n"
142                 + "(3.1) One Param(s,l): " + short2.format(date) +"\n"
143                 + "(3.2) One Param(m,l): " + medium2.format(date) +"\n"
144                 + "(3.3) One Param(l,l): " + long2.format(date) +"\n"
145                 + "(3.4) One Param(f,l): " + full2.format(date) +"\n"
146                 ); 
147 
148     }
149 
150 
151     /**
152      * 测试DateFormat的format()函数
153      */
154     public static void testFormat() {
155         Date date = new Date(); 
156         StringBuffer sb = new StringBuffer();
157         FieldPosition field = new FieldPosition(DateFormat.YEAR_FIELD);
158         DateFormat format = DateFormat.getDateTimeInstance();
159 
160         sb =  format.format(date, sb, field);
161         System.out.println("\ntestFormat"); 
162         System.out.printf("sb=%s\n", sb);
163     }
164 }
View Code

运行结果:

----getTimeInstance ----
(1.0) Empty Param   : 4:54:22 PM
(2.1) One Param(s)  : 4:54 PM
(2.2) One Param(m)  : 4:54:22 PM
(2.3) One Param(l)  : 4:54:22 PM CST
(2.4) One Param(f)  : 4:54:22 PM CST
(3.1) One Param(s,l): 下午4:54
(3.2) One Param(m,l): 16:54:22
(3.3) One Param(l,l): 下午04时54分22秒
(3.4) One Param(f,l): 下午04时54分22秒 CST


----getDateInstance ----
(1.0) Empty Param   : Jan 23, 2014
(2.1) One Param(s)  : 1/23/14
(2.2) One Param(m)  : Jan 23, 2014
(2.3) One Param(l)  : January 23, 2014
(2.4) One Param(f)  : Thursday, January 23, 2014
(3.1) One Param(s,l): 14-1-23
(3.2) One Param(m,l): 2014-1-23
(3.3) One Param(l,l): 2014年1月23日
(3.4) One Param(f,l): 2014年1月23日 星期四


----getDateTimeInstance ----
(1.0) Empty Param   : Jan 23, 2014 4:54:23 PM
(2.1) One Param(s)  : 1/23/14 4:54 PM
(2.2) One Param(m)  : Jan 23, 2014 4:54:23 PM
(2.3) One Param(l)  : January 23, 2014 4:54:23 PM CST
(2.4) One Param(f)  : Thursday, January 23, 2014 4:54:23 PM CST
(3.1) One Param(s,l): 14-1-23 下午4:54
(3.2) One Param(m,l): 2014-1-23 16:54:23
(3.3) One Param(l,l): 2014年1月23日 下午04时54分23秒
(3.4) One Param(f,l): 2014年1月23日 星期四 下午04时54分23秒 CST


testFormat
sb=Jan 23, 2014 4:54:23 PM

 

OK。至此,对DateFormat的学习到此为止。接下来,我们开始学习SimpleDateFormat,它才是格式化Date需要重点了解的。

 


更多内容

Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(1) Calendar

Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(2) 自己封装的Calendar接口

Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(3) Date

Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(4) DateFormat

 

posted on 2013-10-10 09:04  如果天空不死  阅读(2636)  评论(0编辑  收藏  举报