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

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

转换符 说明
%te 一年中的某一天
%tb 指定语言环境的月份简称
%tB 指定语言环境的月份全称
%tA 指定语言环境的星期几全称
%ta 指定语言环境的星期几简称
%tc 包括全部日期和时间信息
%tY 4位年份
%tj 一年中的第几天
%tm 月份
%td 一个月中的第几天
%ty 2位年份
%tH 2位数字的24时制的小时
%tI 2位数字的12时制的小时
%tk 2位数字的24时制的小时
%tl 2位数字的12时制的小时
%tM 2位数字的分钟
%tS 2位数字的秒数
%tL 2位数字的毫秒数
%tN 9位数字的微秒数
%tp 指定语言环境上午或下午标记
%tz 相对于 GMT RFC 82 格式的数字时区偏移量
%tZ 时区缩写形式的字符串
%ts 1970-01-01 00:00:00 至现在经过的秒数
%tQ 1970-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));
        
        
    }
}

输出结果

输出结果

猜你喜欢

转载自www.cnblogs.com/gcvition/p/12105445.html
今日推荐