Java common date and time format conversion specifier

Common date and time format conversion specifier

The conversion operation Explanation
%te One day of the year
%tb Month referred to the specified locale
%tB Full month name for the specified locale
%tA Specifies the full name of the locale of the week
%ta A few weeks short of the specified locale
%tc All information including the date and time
%tY 4 Year
%tj The first day of the year
%tm month
%td The first day of the month
%ty 2 Year
%tH 24 made of two-digit hour
%tI 12 made of two-digit hour
%tk 24 made of two-digit hour
%tl 12 made of two-digit hour
%tM A two-digit minutes
%tS 2-digit number of seconds
%tL A two-digit number of milliseconds
%tN Figure 9 microseconds
%tp Specified locale morning or afternoon marker
%tz With respect to the digital format GMT RFC 82 zone offset
%tZ Timezone String abbreviated form
%ts 1970-01-01 00:00:00 to now the number of seconds
%tQ 1970-01-01 00:00:00 to now the number of milliseconds elapsed
%tF "Year - Month - Day" format (4 years)
%tD "Month / day / year" format (two years)
%tr "Hh: mm: ss AM / PM" format (12 hour)
%tT "Hour: minute: second" format (24) was
%tR "Hours: minutes" format (24) was

Code examples

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));
        
        
    }
}

Output

Output

Guess you like

Origin www.cnblogs.com/gcvition/p/12105445.html