getDateInstance():返回一个日期格式器,它格式化后的字符串只有日期,没有时间。
getTimeInstance():返回一个时间格式器,它格式化后的字符串只有时间,没有日期。
getDateTimeInstance():返回一个日期、时间格式器,它格式化后的字符串既有日期,也有日期。
import java.util.*;
import java.text.*;{
public static void main(String[] args)
{
Date dt = new Date();
Locale[] locales = {Locale.CHINA, Locale.US};
DateFormat[] df = new DateFormat[16];
System.out.println(dt);
System.out.println(df);
for(int i = 0; i < locales.length ; i++)
{
df[i * 8] = DateFormat.getDateInstance(DateFormat.SHORT, locales[i]);
df[i * 8 +1] = DateFormat.getDateInstance(DateFormat.MEDIUM, locales[i]);
df[i * 8 +2] = DateFormat.getDateInstance(DateFormat.LONG, locales[i]);
df[i * 8 +3] = DateFormat.getDateInstance(DateFormat.FULL, locales[i]);
df[i * 8 +4] = DateFormat.getTimeInstance(DateFormat.SHORT, locales[i]);
df[i * 8 +5] = DateFormat.getTimeInstance(DateFormat.MEDIUM, locales[i]);
df[i * 8 +6] = DateFormat.getTimeInstance(DateFormat.LONG, locales[i]);
df[i * 8 +7] = DateFormat.getTimeInstance(DateFormat.FULL, locales[i]);
}
for(int i = 0; i < locales.length; i++)
{
switch(i)
{
case 0:
System.out.println("--------中国日期格式----------");
break;
case 1:
System.out.println("--------美国日期格式----------");
break;
}
System.out.println("SHORT格式的日期格式:" + df[i*8].format(dt));
System.out.println("MEDIUM格式的日期格式:" + df[i*8+1].format(dt));
System.out.println("LONG格式的日期格式:" + df[i*8+2].format(dt));
System.out.println("FULL格式的日期格式:" + df[i*8+3].format(dt));
System.out.println("SHORT格式的日期格式:" + df[i*8+4].format(dt));
System.out.println("MEDIUM格式的日期格式:" + df[i*8+5].format(dt));
System.out.println("LONG格式的日期格式:" + df[i*8+6].format(dt));
System.out.println("FULL格式的日期格式:" + df[i*8+7].format(dt));
}
}
}
运行结果:
Sat Jul 19 22:15:03 CST 2014
[Ljava.text.DateFormat;@5ddb6e
--------中国日期格式----------
SHORT格式的日期格式:14-7-19
MEDIUM格式的日期格式:2014-7-19
LONG格式的日期格式:2014年7月19日
FULL格式的日期格式:2014年7月19日 星期六
SHORT格式的日期格式:下午10:15
MEDIUM格式的日期格式:22:15:03
LONG格式的日期格式:下午10时15分03秒
FULL格式的日期格式:下午10时15分03秒 CST
--------美国日期格式----------
SHORT格式的日期格式:7/19/14
MEDIUM格式的日期格式:Jul 19, 2014
LONG格式的日期格式:July 19, 2014
FULL格式的日期格式:Saturday, July 19, 2014
SHORT格式的日期格式:10:15 PM
MEDIUM格式的日期格式:10:15:03 PM
LONG格式的日期格式:10:15:03 PM CST
FULL格式的日期格式:10:15:03 PM CST