Java常用日期、时间格式化转换符

常用日期、时间格式化转换符

转换符说明
%te一年中的某一天
%tb指定语言环境的月份简称
%tB指定语言环境的月份全称
%tA指定语言环境的星期几全称
%ta指定语言环境的星期几简称
%tc包括全部日期和时间信息
%tY4位年份
%tj一年中的第几天
%tm月份
%td一个月中的第几天
%ty2位年份
%tH2位数字的24时制的小时
%tI2位数字的12时制的小时
%tk2位数字的24时制的小时
%tl2位数字的12时制的小时
%tM2位数字的分钟
%tS2位数字的秒数
%tL2位数字的毫秒数
%tN9位数字的微秒数
%tp指定语言环境上午或下午标记
%tz相对于 GMT RFC 82 格式的数字时区偏移量
%tZ时区缩写形式的字符串
%ts1970-01-01 00:00:00 至现在经过的秒数
%tQ1970-01-01 00:00:00 至现在经过的毫秒数
%tF“年-月-日”格式(4位年份)
%tD“月/日/年”格式(2位年份)
%tr“时:分:秒 上午/下午”格式(12时制)
%tT“时:分:秒”格式(24时制)
%tR“时:分”格式(24时制)

代码实例

public class StringDateFormat {
    public static void main(String[] args) {
        Date date = new Date();
        //日期
        System.out.println("%te 一年中的某一天 ---> "+String.format("%te", date));
        System.out.println("%tb 指定语言环境的月份简称 ---> "+String.format("%tb", date));
        System.out.println("%tB 指定语言环境的月份全称 ---> "+String.format("%tB", date));
        System.out.println("%tA 指定语言环境的星期几全称 ---> "+String.format("%tA", date));
        System.out.println("%ta 指定语言环境的星期几简称 ---> "+String.format("%ta", date));
        System.out.println("%tc 包括全部日期和时间信息 ---> "+String.format("%tc", date));
        System.out.println("%tY 4位年份 ---> "+String.format("%tY", date));
        System.out.println("%ty 2位年份 ---> "+String.format("%ty", date));
        System.out.println("%tm 月份 ---> "+String.format("%tm", date));
        System.out.println("%td 一个月中的第几天 ---> "+String.format("%td", date));
        System.out.println("%tF “年-月-日”格式(4位年份) ---> "+String.format("%tF", date));
        System.out.println("%tD “年-月-日”格式(2位年份) ---> "+String.format("%tD", date));
        
        System.out.println("-------------------------------------------------------------------");
        
        //时间
        System.out.println("%tH 2位数字的24时制的小时 ---> "+String.format("%tH", date));
        System.out.println("%tI 2位数字的12时制的小时 ---> "+String.format("%tI", date));
        System.out.println("%tk 2位数字的24时制的小时 ---> "+String.format("%tk", date));
        System.out.println("%tl 2位数字的12时制的小时 ---> "+String.format("%tl", date));
        System.out.println("%tM 2位数字的分钟 ---> "+String.format("%tM", date));
        System.out.println("%tS 2位数字的秒数 ---> "+String.format("%tS", date));
        System.out.println("%tL 2位数字的毫秒数 ---> "+String.format("%tL", date));
        System.out.println("%tN 2位数字的微秒数 ---> "+String.format("%tN", date));
        System.out.println("%tp 指定语言环境上午或下午标记 ---> "+String.format("%tp", date));
        System.out.println("%tz 相对于 GMT RFC 82 格式的数字时区偏移量 ---> "+String.format("%tz", date));
        System.out.println("%tZ 时区缩写形式的字符串 ---> "+String.format("%tZ", date));
        System.out.println("%ts 1970-01-01 00:00:00 至现在经过的秒数 ---> "+String.format("%ts", date));
        System.out.println("%tQ 1970-01-01 00:00:00 至现在经过的毫秒数 ---> "+String.format("%tQ", date));
        System.out.println("%tr “时:分:秒 上午/下午”格式(12时制) ---> "+String.format("%tr", date));
        System.out.println("%tT “时:分:秒”格式(24时制) ---> "+String.format("%tT", date));
        System.out.println("%tR “时:分”格式(24时制) ---> "+String.format("%tR", date));
        
        
    }
}

输出结果

8917395-32cd648009ef3414.png
输出结果
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值