JavaDemo——格式化SimpleDateFormat、DecimalFormat、ChoiceFormat、MessageFormat、String.format

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FlyLikeButterfly/article/details/82260834

格式化时间:

SimpleDateFormat的使用

/**
 * createtime : 2018年8月31日 下午5:21:05
 */
package formattest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * TODO
 * @author XWF
 */
public class TestSimpleDateFormat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Date d = new Date();
		SimpleDateFormat sdformat = new SimpleDateFormat();
		System.out.println("默认:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("y yy yyy yyyy yyyyy");
		System.out.println("年:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("M MM MMM MMMM MMMMM");
		System.out.println("月:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("w ww www wwww wwwww");
		System.out.println("一年中的第几周:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("W WW WWW WWWW WWWWW");
		System.out.println("一个月中的第几周:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("D DD DDD DDDD DDDDD");
		System.out.println("一年中的第几天:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("d dd ddd dddd ddddd");
		System.out.println("一个月中的第几天:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("F FF FFF FFFF FFFFF");
		System.out.println("一个月中的第几周:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("E EE EEE EEEE EEEEE");
		System.out.println("星期:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("a aa aaa aaaa aaaaa",Locale.ENGLISH);//中文格式:上午、下午
		System.out.println("上下午:"+sdformat.format(d));
		sdformat = new SimpleDateFormat("H HH HHH HHHH HHHHH");
		System.out.println("一天中的小时(0-23):"+sdformat.format(d));
		sdformat = new SimpleDateFormat("k kk kkk kkkk kkkkk");
		System.out.println("一天中的小时(1-24):"+sdformat.format(d));
		sdformat = new SimpleDateFormat("K KK KKK KKKK KKKKK");
		System.out.println("上下午的小时(0-11):"+sdformat.format(d));
		sdformat = new SimpleDateFormat("h hh hhh hhhh hhhhh");
		System.out.println("上下午的小时(1-12):"+sdformat.format(d));
		sdformat = new SimpleDateFormat("分钟:m mm mmm mmmm mmmmm");
		System.out.println(sdformat.format(d));
		sdformat = new SimpleDateFormat("秒:s ss sss ssss sssss");
		System.out.println(sdformat.format(d));
		sdformat = new SimpleDateFormat("毫秒:S SS SSS SSSS SSSSS");
		System.out.println(sdformat.format(d));
		sdformat = new SimpleDateFormat("现在是 yyyy年MM月dd日 a HH:mm:ss:sss EEEE");
		System.out.println(sdformat.format(d));
	}

}

结果:

格式化数字:

DecimalFormat和ChoiceFormat的使用

/**
 * createtime : 2018年8月31日 下午5:23:20
 */
package formattest;

import java.text.ChoiceFormat;
import java.text.DecimalFormat;

/**
 * TODO
 * @author XWF
 */
public class TestNumberFormat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DecimalFormat dformat = new DecimalFormat();
		System.out.println("默认3位小数,四舍五入:"+dformat.format(12345.6789));
		dformat = new DecimalFormat("0000.00");//不够补零
		System.out.println(dformat.format(123.456));
		dformat = new DecimalFormat("####.#");//不够忽略
		System.out.println(dformat.format(123.456));
		dformat = new DecimalFormat("-");//负数前缀
		System.out.println(dformat.format(123.456));
		dformat = new DecimalFormat("0.0%");//转换成百分数
		System.out.println(dformat.format(0.123456));
		dformat = new DecimalFormat("0.0000\u2030");//转换成千分数
		System.out.println(dformat.format(0.123456));
		dformat = new DecimalFormat("00,00,00.#");//","分组,不能用于小数部分
		System.out.println(dformat.format(1234567.890));
		//////////////////////////////////////////////////////
		//范围数字格式化
		ChoiceFormat cformat = new ChoiceFormat("1#a|2#b|3#c");//默认左闭右开:[,1)等同[1,2)=a [2,3)=b [3,)=c
		System.out.println("<1:"+cformat.format(0.5));
		System.out.println("1-2:"+cformat.format(1.2));
		System.out.println("=2:"+cformat.format(2));
		System.out.println("2-3:"+cformat.format(2.9));
		System.out.println(">3:"+cformat.format(10));
		double[] limit = {4,5,6};//必升序,\u221E(表示无穷大)
		String[] formats = {"si","wu","lq"};//两数据长度相同
		cformat = new ChoiceFormat(limit,formats);
		System.out.println(cformat.format(3.4));
		System.out.println(cformat.format(5.01));
		System.out.println(cformat.format(6));
	}

}

结果:

MessageFormat的使用

/**
 * createtime : 2018年8月31日 下午5:24:52
 */
package formattest;

import java.text.ChoiceFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO
 * @author XWF
 */
public class TestMessageFormat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//{参数索引}或者{参数索引,类型}或者{参数索引,类型,模式}
		//类型:number,date,time,choice;
		//模式:short,medium,long,full,integer,currency,percent,SubformatPattern(子模式)
		//number<->NumberFormat
		//date和time<->DateFormat
		//choice<->ChoiceFormat
		System.out.println(MessageFormat.format("格式化第一个Date:{0,date,yyyy年MM月dd日},格式化第二个数字:{1,number,0.00},格式化string:{2}", new Date(),1234.5678,"String"));
		
		//指定
		MessageFormat mformat = new MessageFormat("要格式化的参数:{0}和参数:{1}");
		Object[] arguments = {2.5 , new Date()};//参数
		ChoiceFormat cformat = new ChoiceFormat("1#aAa|2#bBb|3#cCc");
		mformat.setFormatByArgumentIndex(0, cformat);//指定0参数用ChoiceFormat
		SimpleDateFormat sdformat = new SimpleDateFormat("yyyy年MM月");
		mformat.setFormatByArgumentIndex(1, sdformat);//指定1参数用SimpleDateFormat
		System.out.println(mformat.format(arguments));
	}

}

结果:

字符串格式化:

String.format(String format, Object... args)

String.format(Locale l, String format, Object... args)

/**
 * createtime : 2018年8月31日 下午5:43:05
 */
package formattest;

import java.util.Date;
import java.util.Locale;

/**
 * TODO
 * @author XWF
 */
public class TestStringFormat {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(String.format("字符串:%s", "字符串"));
		System.out.println(String.format("字符:%c", 'c'));
		System.out.println(String.format("布尔:%b", 0));
		System.out.println(String.format("十进制整数:%d", 123));
		System.out.println(String.format("十六进制整数:%x", 17));
		System.out.println(String.format("八进制整数:%o", 9));
		System.out.println(String.format("浮点数:%f", 123.456));
		System.out.println(String.format("十六进制浮点数:%a", 6.123));
		System.out.println(String.format("指数:%e", 123.456789));
		System.out.println(String.format("通用浮点(f和e中较短者):%g", 123.4));
		System.out.println(String.format("散列码:%h", 'A'));
		System.out.println(String.format("百分号:%%"));
		System.out.println(String.format("换行%n"));
		
		System.out.println(String.format("+正负数添加符号:%+d %+d", 12,-33));
		System.out.println(String.format("-左对齐:XXXX%-8dXXXX", 31));
		System.out.println(String.format("0前补0:%03d", 2));
		System.out.println(String.format(" 前补空格:XXXX% 4dXXXX", 34));
		System.out.println(String.format(",分组:%,f", 123456789.123456789));
		System.out.println(String.format("(括号括负数:%(d %(d",123,-123));
		System.out.println(String.format("#如果是浮点数则包含小数点,如果是16进制或8进制则添加0x或0:%#.0f %#x %#o", 123f,16,8));
		System.out.println(String.format("<格式化之前的:%.1f %<.3f %<.4f", 123.456));
		System.out.println(String.format("$格式化指定位置参数:%2$s %3$.2f %1$+d",1,"二",333.333));
		System.out.println();
		/////////////////////////////////////////////////////////////
		Date d = new Date();
		System.out.println("现在:"+d);
		//日期
		System.out.println(String.format("月份简称:%tb", d));
		System.out.println(String.format("月份全称:%tB", d));
		System.out.println(String.format("周几简称:%ta", d));
		System.out.println(String.format("周几全称:%tA", d));
		System.out.println(String.format("前2位数年份:%tC", d));
		System.out.println(String.format("后2位数年份:%ty", d));
		System.out.println(String.format("4位数年份:%tY", d));
		System.out.println(String.format("月份:%tm", d));
		System.out.println(String.format("月份中的某一天(1位不补0)(1-31):%te", d));
		System.out.println(String.format("月份中的某一天(两位补0)(01-31):%td", d));
		System.out.println(String.format("一年中的第几天:%tj", d));
		//时间
		System.out.println(String.format("小时(00-23):%tH", d));
		System.out.println(String.format("小时(01-12):%tI", d));
		System.out.println(String.format("小时(0-23):%tk", d));
		System.out.println(String.format("小时(1-12):%tl", d));
		System.out.println(String.format("分钟(00-59):%tM", d));
		System.out.println(String.format("秒(00-59):%tS", d));
		System.out.println(String.format("毫秒(000-999):%tL", d));
		System.out.println(String.format("微妙(000000000-999999999):%tN", d));
		System.out.println(String.format("上下午:%tp", d));
		System.out.println(String.format("时区偏移(+0800):%tz", d));
		System.out.println(String.format("时区缩写(CST):%tZ", d));
		System.out.println(String.format("从1970-01-01 00:00:00 到现在的秒:%ts", d));
		System.out.println(String.format("从1970-01-01 00:00:00 到现在的毫秒:%tQ", d));
		//时间和日期
		System.out.println(String.format("年-月-日:%tF", d));
		System.out.println(String.format("月/日/年:%tD", d));
		System.out.println(String.format("完整日期时间:%tc", d));
		System.out.println(String.format("时分秒(12h) am/pm:%tr", d));
		System.out.println(String.format("时分秒(24h):%tT", d));
		System.out.println(String.format("时分(24h):%tR", d));
		System.out.println(String.format(Locale.ENGLISH,"完整日期时间:%tc", d));
	}

}

结果:

参考:

https://blog.csdn.net/joenqc/article/details/56014679

https://www.cnblogs.com/Dhouse/p/7776780.html

https://blog.csdn.net/gr912308719/article/details/80299898

猜你喜欢

转载自blog.csdn.net/FlyLikeButterfly/article/details/82260834