Date 及 Number 的格式化

文章目录

Date

// 2021-09-29 下午
Date date = new Date();
System.out.println(date); // Wed Sep 29 18:59:53 CST 2021
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("G"); // 公元 Era 标志符
dateFormat = new SimpleDateFormat("y"); // 2021 年
dateFormat = new SimpleDateFormat("M"); // 9 年中的月份
dateFormat = new SimpleDateFormat("w"); // 40 年中的周数
dateFormat = new SimpleDateFormat("W"); // 5 月份中的周数
dateFormat = new SimpleDateFormat("D"); // 272 年中的天数
dateFormat = new SimpleDateFormat("d"); // 29 月份中的天数
dateFormat = new SimpleDateFormat("F"); // 5 月份中的星期
dateFormat = new SimpleDateFormat("E"); // 星期三 星期中的天数
dateFormat = new SimpleDateFormat("a"); // 下午 Am/pm 标记
dateFormat = new SimpleDateFormat("H"); // 18 一天中的小时数(0-23)
dateFormat = new SimpleDateFormat("k"); // 18 一天中的小时数(1-24)
dateFormat = new SimpleDateFormat("K"); // 6 am/pm 中的小时数(0-11)
dateFormat = new SimpleDateFormat("h"); // 6 am/pm 中的小时数(1-12)
dateFormat = new SimpleDateFormat("m"); // 59 小时中的分钟数
dateFormat = new SimpleDateFormat("s"); // 53 分钟中的秒数
dateFormat = new SimpleDateFormat("S"); // 712 毫秒数
dateFormat = new SimpleDateFormat("z"); // CST 时区
dateFormat = new SimpleDateFormat("Z"); // +0800 时区

dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
System.out.println(dateFormat.format(date));  // 2021-09-29 19:07:18
System.out.println(date); // Wed Sep 29 19:07:18 CST 2021

System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-09-29 19:07:18")); // Wed Sep 29 19:07:18 CST 2021

String input = "2012-12-31";
String pattern = "yyyy-MM-dd";
DateFormat dateFormat = new SimpleDateFormat(pattern);
System.out.println(dateFormat.parse(input)); // Mon Dec 31 00:00:00 CST 2012

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-09-29 19:07:18");
System.out.println(date); // Wed Sep 29 19:07:18 CST 2021

System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-09-29 19:07:18"))); // 2021-09-29 19:07:18
字母日期或时间元素表示示例
GEra 标志符TextAD
yYear1996; 96
M年中的月份MonthJuly; Jul; 07
w年中的周数Number27
W月份中的周数Number2
D年中的天数Number189
d月份中的天数Number10
F月份中的星期Number2
E星期中的天数TextTuesday; Tue
aAm/pm 标记TextPM
H一天中的小时数(0-23)Number0
k一天中的小时数(1-24)Number24
Kam/pm 中的小时数(0-11)Number0
ham/pm 中的小时数(1-12)Number12
m小时中的分钟数Number30
s分钟中的秒数Number55
S毫秒数Number978
z时区General time zonePacific Standard Time; PST; GMT-08:00
Z时区RFC 822 time zone-0800

Number

double num = 10234567.895;
NumberFormat numberFormat = new DecimalFormat("¥###,###,###.##"); // ¥10,234,567.89
numberFormat = new DecimalFormat("¥000,000,000.00"); // ¥010,234,567.89
numberFormat = new DecimalFormat("¥###,000,000.0"); // ¥10,234,567.9
numberFormat = new DecimalFormat("¥###,000,000.00"); // ¥10,234,567.89
numberFormat = new DecimalFormat("¥###,000,000"); // ¥10,234,568
System.out.println(numberFormat.format(num));

int code = 2;
NumberFormat numberFormat = new DecimalFormat("00000"); // 00002
System.out.println(numberFormat.format(code));
System.out.println(new DecimalFormat("00000").format(30)); // 00030

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧梦时节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值