使用NumberFormat格式化数字:
public class NumberFormatTest
{
public static void main(String[] args)
{
// 需要被格式化的数字
double db = 1234000.567;
// 创建四个Locale,分别代表中国、日本、德国、美国
Locale[] locales = {Locale.CHINA, Locale.JAPAN
, Locale.GERMAN, Locale.US};
NumberFormat[] nf = new NumberFormat[12];
// 为上面四个Locale创建12个NumberFormat对象
// 每个Locale分别有通用数值格式器、百分比格式器、货币格式器
for (int i = 0 ; i < locales.length ; i++)
{
nf[i * 3] = NumberFormat.getNumberInstance(locales[i]);
nf[i * 3 + 1] = NumberFormat.getPercentInstance(locales[i]);
nf[i * 3 + 2] = NumberFormat.getCurrencyInstance(locales[i]);
}
for (int i = 0 ; i < locales.length ; i++)
{
String tip = i == 0 ? "----中国的格式----" :
i == 1 ? "----日本的格式----" :
i == 2 ? "----德国的格式----" :"----美国的格式----";
System.out.println(tip);
System.out.println("通用数值格式:"
+ nf[i * 3].format(db));
System.out.println("百分比数值格式:"
+ nf[i * 3 + 1].format(db));
System.out.println("货币数值格式:"
+ nf[i * 3 + 2].format(db));
}
}
}
使用DateFormat格式化日期,时间
public class DateFormatTest
{
public static void main(String[] args)
throws ParseException
{
// 需要被格式化的时间
Date dt = new Date();
// 创建两个Locale,分别代表中国、美国
Locale[] locales = {Locale.CHINA, Locale.US};
DateFormat[] df = new DateFormat[16];
// 为上面两个Locale创建16个DateFormat对象
for (int i = 0 ; i < locales.length ; i++)
{
df[i * 8] = DateFormat.getDateInstance(SHORT, locales[i]);
df[i * 8 + 1] = DateFormat.getDateInstance(MEDIUM, locales[i]);
df[i * 8 + 2] = DateFormat.getDateInstance(LONG, locales[i]);
df[i * 8 + 3] = DateFormat.getDateInstance(FULL, locales[i]);
df[i * 8 + 4] = DateFormat.getTimeInstance(SHORT, locales[i]);
df[i * 8 + 5] = DateFormat.getTimeInstance(MEDIUM , locales[i]);
df[i * 8 + 6] = DateFormat.getTimeInstance(LONG , locales[i]);
df[i * 8 + 7] = DateFormat.getTimeInstance(FULL , locales[i]);
}
for (int i = 0 ; i < locales.length ; i++)
{
String tip = i == 0 ? "----中国日期格式----":"----美国日期格式----";
System.out.println(tip);
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));
}
String str1 = "2014-12-12";
String str2 = "2014年12月10日";
// 下面输出 Fri Dec 12 00:00:00 CST 2014
System.out.println(DateFormat.getDateInstance().parse(str1));
// 下面输出 Wed Dec 10 00:00:00 CST 2014
System.out.println(DateFormat.getDateInstance(LONG).parse(str2));
// 下面抛出 ParseException异常
// System.out.println(DateFormat.getDateInstance().parse(str2));
}
}
----中国日期格式----
SHORT格式的日期格式:17-6-7
MEDIUM格式的日期格式:2017-6-7
LONG格式的日期格式:2017年6月7日
FULL格式的日期格式:2017年6月7日 星期三
SHORT格式的时间格式:下午6:48
MEDIUM格式的时间格式:18:48:24
LONG格式的时间格式:下午06时48分24秒
FULL格式的时间格式:下午06时48分24秒 CST
----美国日期格式----
SHORT格式的日期格式:6/7/17
MEDIUM格式的日期格式:Jun 7, 2017
LONG格式的日期格式:June 7, 2017
FULL格式的日期格式:Wednesday, June 7, 2017
SHORT格式的时间格式:6:48 PM
MEDIUM格式的时间格式:6:48:24 PM
LONG格式的时间格式:6:48:24 PM CST
FULL格式的时间格式:6:48:24 PM CST
Fri Dec 12 00:00:00 CST 2014
Wed Dec 10 00:00:00 CST 2014
使用SimpleDateFormat格式化日期
public class SimpleDateFormatTest
{
public static void main(String[] args)
throws ParseException
{
Date d = new Date();
// 创建一个SimpleDateFormat对象
SimpleDateFormat sdf1 = new SimpleDateFormat("Gyyyy年中第D天");
// 将d格式化成日期,输出:公元2014年中第101天
String dateStr = sdf1.format(d);
System.out.println(dateStr);
// 一个非常特殊的日期字符串
String str = "14###三月##21";
SimpleDateFormat sdf2 = new SimpleDateFormat("y###MMM##d");
// 将日期字符串解析成日期,输出:Fri Mar 21 00:00:00 CST 2014
System.out.println(sdf2.parse(str));
}
}
公元2017年中第158天
Fri Mar 21 00:00:00 CST 2014
使用Java8新增的DateTimeFormatter格式器完成格式化:
public class NewFormatterTest
{
public static void main(String[] args)
{
DateTimeFormatter[] formatters = new DateTimeFormatter[]{
// 直接使用常量创建DateTimeFormatter格式器
DateTimeFormatter.ISO_LOCAL_DATE,
DateTimeFormatter.ISO_LOCAL_TIME,
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
// 使用本地化的不同风格来创建DateTimeFormatter格式器
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM),
DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG),
// 根据模式字符串来创建DateTimeFormatter格式器
DateTimeFormatter.ofPattern("Gyyyy%%MMM%%dd HH:mm:ss")
};
LocalDateTime date = LocalDateTime.now();
// 依次使用不同的格式器对LocalDateTime进行格式化
for(int i = 0 ; i < formatters.length ; i++)
{
// 下面两行代码的作用相同
System.out.println(date.format(formatters[i]));
System.out.println(formatters[i].format(date));
}
}
}
2017-06-07
2017-06-07
18:52:00.966
18:52:00.966
2017-06-07T18:52:00.966
2017-06-07T18:52:00.966
2017年6月7日 星期三 18:52:00
2017年6月7日 星期三 18:52:00
下午06时52分00秒
下午06时52分00秒
公元2017%%六月%%07 18:52:00
公元2017%%六月%%07 18:52:00
使用DateTimeFormatter解析字符串:
public class NewFormatterParse
{
public static void main(String[] args)
{
// 定义一个任意格式的日期时间字符串
String str1 = "2014==04==12 01时06分09秒";
// 根据需要解析的日期、时间字符串定义解析所用的格式器
DateTimeFormatter fomatter1 = DateTimeFormatter
.ofPattern("yyyy==MM==dd HH时mm分ss秒");
// 执行解析
LocalDateTime dt1 = LocalDateTime.parse(str1, fomatter1);
System.out.println(dt1); // 输出 2014-04-12T01:06:09
// ---下面代码再次解析另一个字符串---
String str2 = "2014$$$四月$$$13 20小时";
DateTimeFormatter fomatter2 = DateTimeFormatter
.ofPattern("yyy$$$MMM$$$dd HH小时");
LocalDateTime dt2 = LocalDateTime.parse(str2, fomatter2);
System.out.println(dt2); // 输出 2014-04-13T20:00
}
}
2014-04-12T01:06:09
2014-04-13T20:00