String.format() 格式化字符串

str.format(String format,Object args)
format:格式化转换符
args:格式化字符串中格式说明符引用的参数。(数目可变,可为0)
返回格式化后的字符串,格式化后的字符串使用本地默认的语言环境
str.format(Local l,String format,Object args)
l:格式化过程中要应用的语言环境,如l为null,则不进行本地化。
format:格式化转换符
args:格式化字符串中格式说明符引用的参数。(数目可变,可为0)
返回格式化后的字符串


日期格式化
常用日期格式化转换符:

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

public class Demo1 {
	public static void main(String[] args){
		Date today = new Date();
		String a = String.format(Locale.US, "%tb",today);//格式化月份英文缩写
		String b = String.format(Locale.US, "%tB", today);//格式化月份英文全写
		String c = String.format(Locale.CHINA, "%tB", today);//格式化中文月份
		String d = String.format(Locale.US,"%ta", today);//格式化星期几英文简称
		String e = String.format(Locale.US,"%tA", today);//格式化星期几英文全称
		String f = String.format("%tY", today);//格式化4位年份
		String g = String.format("%ty", today);//格式化2位年份
		String h = String.format("%tj", today);//格式化一年中的第几天
		String i = String.format("%tm", today);//格式化月份
		String j = String.format("%td", today);//格式化一个月中的第几天
		String k = String.format("%te", today);//格式化一个月中的某天
		String l = String.format("%tc", today);//格式化所有日期和时间信息
		
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
		System.out.println(f);
		System.out.println(g);
		System.out.println(h);
		System.out.println(i);
		System.out.println(j);
		System.out.println(k);
		System.out.println(l);
	}
}

时间格式化
常用时间格式化转换符:


日期时间组合格式化



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值