看核心一时,感觉String.format( )方法对获取时间相对简易,缺点是只能获取字符串,所以做个记录。
主要需要注意的是格式,Locale参数可以选择各个国家不同格式
String.format("%tc", new Date()) ----- 完整时间 :星期四 四月 25 22:53:47 CST 2019
String.format("%tF", new Date()) ----- 年月日 :2019-04-25
String.format("%tD", new Date()) ----- 月日年 :04/25/19
String.format("%tT", new Date()) ----- 24小时制 :22:53:47
String.format("%tr", new Date()) ----- 12小时制 :10:53:47 下午 (添加Locale.ENGLISH参数,上下午是am/pm)
String.format("%tR", new Date()) ----- 24小时没有秒 :22:53
String.format("%tC", new Date()) ----- 年份前两位(补0) :20
String.format("%ty", new Date()) ----- 年份后两位(补0) :19
String.format("%tY", new Date()) ----- 年份 :2019
String.format("%tB", new Date()) ----- 中文月份 :四月(添加Locale.JAPAN参数,显示的是 4月)
String.format(Locale.ENGLISH, "%tB", new Date()) ----- 英文月份 :April
String.format(Locale.ENGLISH, "%tb", new Date()) ----- 英文月份缩写 :Apr
String.format("%tm", new Date()) ----- 数字月份(补0):04
String.format("%td", new Date()) ----- 数字日期(补0):25
String.format("%te", new Date()) ----- 数字日期(不补0):25
String.format("%tA", new Date()) ----- 中文星期 :星期四
String.format(Locale.ENGLISH, "%tA", new Date()) ----- 英文星期 :Thursday
String.format(Locale.ENGLISH, "%ta", new Date()) ----- 英文星期缩写 :Thu
String.format("%tH", new Date()) ----- 24制小时(补0):22
String.format("%tk", new Date()) ----- 24制小时(不补0):22
String.format("%tI", new Date()) ----- 12制小时(补0):10
String.format("%tl", new Date()) ----- 12制小时(补0):10
String.format("%tM", new Date()) ----- 分钟(补0) :53
String.format("%tS", new Date()) ----- 秒钟(补0) :47
String.format("%tp", new Date()) ----- 上下午标志 :下午(添加Locale.ENGLISH参数,上下午是am/pm)
String.format("%tZ", new Date()) ----- 时区 :CST