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

本章主要介绍DateFormat。

 


DateFormat 介绍

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

 

DateFormat 的作用是格式化Date。它支持格式化风格包括 FULLLONGMEDIUM 和 SHORT 共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中的定义如下:

public static final int FULL = 0;
public static final int LONG = 1;
public static final int MEDIUM = 2;
public static final int SHORT = 3;
public static final int DEFAULT = MEDIUM;

public final static DateFormat getInstance() {
    return getDateTimeInstance(SHORT, SHORT);
}

public final static DateFormat getTimeInstance()
{
    return get(DEFAULT, 0, 1, Locale.getDefault());
}

public final static DateFormat getTimeInstance(int style)
{
    return get(style, 0, 1, Locale.getDefault());
}

public final static DateFormat getTimeInstance(int style,
                                             Locale aLocale)
{
    return get(style, 0, 1, aLocale);
}

public final static DateFormat getDateInstance()
{
    return get(0, DEFAULT, 2, Locale.getDefault());
}

public final static DateFormat getDateInstance(int style)
{
    return get(0, style, 2, Locale.getDefault());
}

public final static DateFormat getDateInstance(int style,
                                             Locale aLocale)
{
    return get(0, style, 2, aLocale);
}

public final static DateFormat getDateTimeInstance()
{
    return get(DEFAULT, DEFAULT, 3, Locale.getDefault());
}

public final static DateFormat getDateTimeInstance(int dateStyle,
                                                   int timeStyle)
{
    return get(timeStyle, dateStyle, 3, Locale.getDefault());
}

public final static DateFormat
    getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
{
    return get(timeStyle, dateStyle, 3, aLocale);
}

/**
 * 获取DateFormat实例,实际上是返回SimpleDateFormat对象。
 *
 * timeStyle -- 值可以为“FULL”或“LONG”或“MEDIUM”或“SHORT”
 * dateStyle -- 值可以为“FULL”或“LONG”或“MEDIUM”或“SHORT”
 * flags     -- 值可以为“1”或“2”或“3”。
 *       1 表示获取“时间样式”
 *       2 表示获取“日期样式”
 *       3 表示获取“时间和日期样式”
 * loc       -- locale对象,表示“区域”
 */
private static DateFormat get(int timeStyle, int dateStyle,
                              int flags, Locale loc) {
    if ((flags & 1) != 0) {
        if (timeStyle < 0 || timeStyle > 3) {
            throw new IllegalArgumentException("Illegal time style " + timeStyle);
        }
    } else {
        timeStyle = -1;
    }
    if ((flags & 2) != 0) {
        if (dateStyle < 0 || dateStyle > 3) {
            throw new IllegalArgumentException("Illegal date style " + dateStyle);
        }
    } else {
        dateStyle = -1;
    }
    try {
        // Check whether a provider can provide an implementation that's closer
        // to the requested locale than what the Java runtime itself can provide.
        LocaleServiceProviderPool pool =
            LocaleServiceProviderPool.getPool(DateFormatProvider.class);
        if (pool.hasProviders()) {
            DateFormat providersInstance = pool.getLocalizedObject(
                                                DateFormatGetter.INSTANCE,
                                                loc,
                                                timeStyle,
                                                dateStyle,
                                                flags);
            if (providersInstance != null) {
                return providersInstance;
            }
        }

        return new SimpleDateFormat(timeStyle, dateStyle, loc);
    } catch (MissingResourceException e) {
        return new SimpleDateFormat("M/d/yy h:mm a");
    }
}

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

 


DateFormat 实例

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

import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.FieldPosition;

/**
 * DateFormat 的API测试程序
 *
 * @author skywang
 * @email kuiwu-wang@163.com
 */
public class DateFormatTest {

    public static void main(String[] args) {

        // 只显示“时间”:调用getTimeInstance()函数
        testGetTimeInstance() ;

        // 只显示“日期”:调用getDateInstance()函数
        testGetDateInstance() ;

        // 显示“日期”+“时间”:调用getDateTimeInstance()函数
        testGetDateTimeInstance() ;

        // 测试format()函数
        testFormat();
    }

    /**
     * 测试DateFormat的getTimeInstance()函数
     * 它共有3种重载形式:
     * (01) getTimeInstance()
     * (02) getTimeInstance(int style)
     * (03) getTimeInstance(int style, Locale locale)
     *
     * @author skywang
     */
    private static void testGetTimeInstance() {
        Date date = new Date();

        //Locale locale = new Locale("fr", "FR");
        Locale locale = new Locale("zh", "CN");

        // 等价于 DateFormat.getTimeInstance( DateFormat.MEDIUM);
        DateFormat short0  = DateFormat.getTimeInstance( );

        // 参数是:“时间的显示样式”
        DateFormat short1  = DateFormat.getTimeInstance( DateFormat.SHORT);
        DateFormat medium1 = DateFormat.getTimeInstance( DateFormat.MEDIUM);
        DateFormat long1   = DateFormat.getTimeInstance( DateFormat.LONG);
        DateFormat full1   = DateFormat.getTimeInstance( DateFormat.FULL);

        // 参数是:“时间的显示样式” 和 “地区”
        DateFormat short2  = DateFormat.getTimeInstance( DateFormat.SHORT, locale);
        DateFormat medium2 = DateFormat.getTimeInstance( DateFormat.MEDIUM, locale);
        DateFormat long2   = DateFormat.getTimeInstance( DateFormat.LONG, locale);
        DateFormat full2   = DateFormat.getTimeInstance( DateFormat.FULL, locale);

        System.out.println("\n----getTimeInstance ----\n"
                + "(1.0) Empty Param   : " + short0.format(date) +"\n"
                + "(2.1) One Param(s)  : " + short1.format(date) +"\n"
                + "(2.2) One Param(m)  : " + medium1.format(date) +"\n"
                + "(2.3) One Param(l)  : " + long1.format(date) +"\n"
                + "(2.4) One Param(f)  : " + full1.format(date) +"\n"
                + "(3.1) One Param(s,l): " + short2.format(date) +"\n"
                + "(3.2) One Param(m,l): " + medium2.format(date) +"\n"
                + "(3.3) One Param(l,l): " + long2.format(date) +"\n"
                + "(3.4) One Param(f,l): " + full2.format(date) +"\n"
                );
    }

    /**
     * 测试DateFormat的getDateTimeInstance()函数
     * 它共有3种重载形式:
     * (01) getDateInstance()
     * (02) getDateInstance(int style)
     * (03) getDateInstance(int style, Locale locale)
     */
    public static void testGetDateTimeInstance() {
        Date date = new Date();

        Locale locale = new Locale("zh", "CN");

        // 等价于 DateFormat.getDateTimeInstance( DateFormat.MEDIUM);
        DateFormat short0  = DateFormat.getDateTimeInstance( );

        DateFormat short1  = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT);
        DateFormat medium1 = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM);
        DateFormat long1   = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG);
        DateFormat full1   = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL);

        DateFormat short2  = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, locale);
        DateFormat medium2 = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
        DateFormat long2   = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG, locale);
        DateFormat full2   = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL, locale);

        System.out.println("\n----getDateTimeInstance ----\n"
                + "(1.0) Empty Param   : " + short0.format(date) +"\n"
                + "(2.1) One Param(s)  : " + short1.format(date) +"\n"
                + "(2.2) One Param(m)  : " + medium1.format(date) +"\n"
                + "(2.3) One Param(l)  : " + long1.format(date) +"\n"
                + "(2.4) One Param(f)  : " + full1.format(date) +"\n"
                + "(3.1) One Param(s,l): " + short2.format(date) +"\n"
                + "(3.2) One Param(m,l): " + medium2.format(date) +"\n"
                + "(3.3) One Param(l,l): " + long2.format(date) +"\n"
                + "(3.4) One Param(f,l): " + full2.format(date) +"\n"
                );
    }

    /**
     * 测试DateFormat的getDateInstance()函数
     * 它共有3种重载形式:
     * (01) getDateTimeInstance()
     * (02) getDateTimeInstance(int dateStyle, int timeStyle)
     * (03) getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
     */
    public static void testGetDateInstance() {
        Date date = new Date();

        //Locale locale = new Locale("en", "US");
        Locale locale = new Locale("zh", "CN");

        // 等价于 DateFormat.getDateInstance( DateFormat.MEDIUM);
        DateFormat short0  = DateFormat.getDateInstance( );

        DateFormat short1  = DateFormat.getDateInstance( DateFormat.SHORT);
        DateFormat medium1 = DateFormat.getDateInstance( DateFormat.MEDIUM);
        DateFormat long1   = DateFormat.getDateInstance( DateFormat.LONG);
        DateFormat full1   = DateFormat.getDateInstance( DateFormat.FULL);

        DateFormat short2  = DateFormat.getDateInstance( DateFormat.SHORT, locale);
        DateFormat medium2 = DateFormat.getDateInstance( DateFormat.MEDIUM, locale);
        DateFormat long2   = DateFormat.getDateInstance( DateFormat.LONG, locale);
        DateFormat full2   = DateFormat.getDateInstance( DateFormat.FULL, locale);

        System.out.println("\n----getDateInstance ----\n"
                + "(1.0) Empty Param   : " + short0.format(date) +"\n"
                + "(2.1) One Param(s)  : " + short1.format(date) +"\n"
                + "(2.2) One Param(m)  : " + medium1.format(date) +"\n"
                + "(2.3) One Param(l)  : " + long1.format(date) +"\n"
                + "(2.4) One Param(f)  : " + full1.format(date) +"\n"
                + "(3.1) One Param(s,l): " + short2.format(date) +"\n"
                + "(3.2) One Param(m,l): " + medium2.format(date) +"\n"
                + "(3.3) One Param(l,l): " + long2.format(date) +"\n"
                + "(3.4) One Param(f,l): " + full2.format(date) +"\n"
                );

    }


    /**
     * 测试DateFormat的format()函数
     */
    public static void testFormat() {
        Date date = new Date();
        StringBuffer sb = new StringBuffer();
        FieldPosition field = new FieldPosition(DateFormat.YEAR_FIELD);
        DateFormat format = DateFormat.getDateTimeInstance();

        sb =  format.format(date, sb, field);
        System.out.println("\ntestFormat");
        System.out.printf("sb=%s\n", sb);
    }
}

运行结果:

----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需要重点了解的。

posted @ 2020-07-29 23:10  跃小云  阅读(119)  评论(0编辑  收藏  举报