java的格式化

来源:互联网 发布:淘宝防排查 编辑:程序博客网 时间:2024/06/06 13:58

java的格式化

java提供一个抽象类Format,它有三个子类分别是MessageFormat,NumberFormat,和DateFormat。它们挑起了java的格式化的大梁,下面就分别看下它们的用法。

一.使用MessageFormat处理包含占位符的字符串

对于带占位符的消息字符串,java提供MessageFormat类可以为占位符指定参数。它有一个静态方法format(String pattern, Object… values),其中pattern是一个带占位符的字符串,后面对象是为占位符指定的参数。

public class Test{public static void main(String[] args){// 字符串占位符从0开始String msg = "hello {0} ,today is {1}";// 第一个参数指定的是第0个占位符,第二个参数指定是第一个占位符,以此类推String fmt = MessageFormat.format(msg,"zhangsan",new Date());System.out.println(fmt);}}

二.使用NumberFormat格式化数字

NumberFormat是一个抽象类,所以无法通过构造器创建NumberFormat对象,它提供了一系列的静态方法得到NumberFormat对象

  1. getCurrencyInstance()
    返回默认Locale的货币格式器,也可以传入指定的Locale
  2. getIntegerInstance()
    返回默认Locale的整数格式器,也可以传入指定的Locale
  3. getNumberInstance()
    返回默认Locale的通用数组格式器,也可以传入指定的Locale
  4. getPercentInstance()
    返回默认Locale的百分数格式器,也可以传入指定的Locale
public class Test{public static void main(String[] args){NumberFormat nft1 = NumberFormat.getCurrencyInstance();NumberFormat nft2 = NumberFormat.getIntegerInstance();NumberFormat nft3 = NumberFormat.getNumberInstance();NumberFormat nft4 = NumberFormat.getPercentInstance();NumberFormat nft5 = NumberFormat.getCurrencyInstance(Locale.FRANCE);double db = 3.1415926;System.out.println("货币格式: "+ nft1.format(db));System.out.println("整数格式:" + nft2.format(db));System.out.println("通用格式:" + nft3.format(db));System.out.println("百分数格式:" + nft4.format(db));System.out.println("法国货币格式:" + nft5.format(db));}}
货币格式:¥3.14整数格式:3通用格式:3.142百分数格式:314%法国货币格式:3,14 €

三.使用DateFormat格式化日期,时间

DateFormat也是一个抽象类,它提供了如下一些静态方法获取DateFormat对象。

  1. getDateInstance()
    返回一个日期格式器,它格式化后的字符串只有日期没有时间。

  2. getTimeInstance()
    返回一个时间格式器,它格式化后的字符串只有时间没有日期。

  3. getDateTimeInstance()
    返回一个日期和时间各格式器,格式化后的字符串既有日期也有时间

上面三个方法都可以传入指定时间日期样式和Locale的参数。日期时间样式参数是DateFormat的4个静态常量:FULL,LONG,MEDIUM,SHORT。

public class Test{public static void main(String[] args){Date dt = new Date();DateFormat df1 = DateFormat.getTimeInstance(DateFormat.FULL);DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL);DateFormat df3 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);DateFormat df4 = DateFormat.getTimeInstance(DateFormat.LONG);DateFormat df5 = DateFormat.getDateInstance(DateFormat.LONG);DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);System.out.println("FULL格式的时间格式 :" + df1.format(dt));System.out.println("FULL格式的日期格式 :" +df2.format(dt));System.out.println("FULL格式的时间日期格式 :" +df3.format(dt));System.out.println("LONG格式的时间格式 :" +df4.format(dt));System.out.println("FULL格式的日期格式 :" +df5.format(dt));System.out.println("FULL格式的时间格式 :" +df6.format(dt));}}
FULL格式的时间格式 :下午041020秒 GMT+08:00FULL格式的日期格式 :2017629日 星期四FULL格式的时间日期格式 :2017629日 星期四 下午041020秒 GMT+08:00LONG格式的时间格式 :下午041020FULL格式的日期格式 :2017629FULL格式的时间格式 :2017629日 下午041020

四.使用SimpleDateFormat格式化日期

SimpleDateFormat是DateFormat的子类,它能够很灵活的格式化日期,也可以用于解析各种格式的日期字符串。

public class Test{public static void main(String[] args) throws ParseException{Date dt = new Date();// 日期格式化成想要的字符串SimpleDateFormat sdf1 = new SimpleDateFormat("今天是yyyy年中的第D天");System.out.println(sdf1.format(dt));//将字符串解析成日期String str = "2017-06-29/16:22:28";SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd/hh:mm:ss");System.out.println(sdf2.parse(str));}}
今天是2017年中的第180天Thu Jun 29 16:22:28 GMT+08:00 2017
原创粉丝点击