栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

格式化类Format

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

格式化类Format

1.DateFormat

日期格式化

SimpleDateFormat(String pattern)

使用给定模式 SimpleDateFormat并使用默认的 FORMAT语言环境的默认日期格式符号。

// SimpleDateFormat(String pattern) 
// 使用给定模式 SimpleDateFormat并使用默认的 FORMAT语言环境的默认日期格式符号。 
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println(date);
// format(Date date) 
// 将给定的 Date成日期/时间字符串,并将结果追加到给定的 StringBuffer 
String date1 = dateFormat.format(date);
System.out.println(date1);

parse(String text) 从字符串中解析文本以产生一个 Date

// parse(String text) 从字符串中解析文本以产生一个 Date 
Date date2 = dateFormat.parse(date1);
System.out.println(date2);

E表示日期中的星期,a表示上/下午,H表示24制小时,h表示12制小时,m表示分钟,s表示秒,S表示毫秒

// E表示日期中的星期
dateFormat = new SimpleDateFormat("yyyy-MM-dd E");
System.out.println(dateFormat.format(date));
// a表示上/下午,H表示24制小时,h表示12制小时,m表示分钟,s表示秒,S表示毫秒
dateFormat = new SimpleDateFormat("yyyy-MM-dd E a HH / hh:mm:ss:S");
System.out.println(dateFormat.format(date));
2.NumberFormat/DecimalFormat

数字格式化NumberFormat/DecimalFormat类

财务数字格式化

// 数字格式化NumberFormat/DecimalFormat类
// 财务数字格式化
double d = 1234567.890;
// #表示有效数字,且最后一位会做四舍五入操作
DecimalFormat decimalFormat = new DecimalFormat("¥###,###,###.##");
System.out.println(decimalFormat.format(d));// ¥1,234,567.89

0表示无论有无效都会显示

// 0表示无论有无效都会显示
decimalFormat = new DecimalFormat("¥000,000,000.000");
System.out.println(decimalFormat.format(d));// ¥001,234,567.890

parse(String source) 从给定字符串的开头解析文本以产生一个数字

// parse(String source) 从给定字符串的开头解析文本以产生一个数字
String s = "¥001,234,567.890";
System.out.println(decimalFormat.parse(s));// 1234567.89
3.MessageForma

文本格式化 MessageFormat类

// 文本格式化
// MessageFormat类
String message = "今天是{0,date},现在是{0,time},我是{1,number}";
MessageFormat messageFormat = new MessageFormat("Date,String");
System.out.println(messageFormat.format(message,new Date(),999));
// 今天是2021-9-28,现在是20:17:25,我是999
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/274713.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号