Java中SimpleDateFormat的使用方法

2025-05-29 0 99

本文内容大多基于官方文档和网上前辈经验总结,经过个人实践加以整理积累,仅供参考。

java.text.SimpleDateFormat 以区域语言环境敏感的方式格式化和解析日期,可以将日期格式化为指定字符串和将字符串解析成日期。

java.text.SimpleDateFormat 可以根据用户定义的模式格式化日期

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36
@Test

public void test() {

Calendar calendar = Calendar.getInstance();

calendar.set(2016, 11, 30, 24, 59, 59);

Date date = new Date(calendar.getTimeInMillis());

String pattern = "G yyyy年MM月dd日HH点mm分ss秒SSS毫秒";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "G yyyy年MM月dd日kk点mm分ss秒SSS毫秒";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "G yyyy年MM月dd日hh点mm分ss秒SSS毫秒 a";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "G yyyy年MM月dd日KK点mm分ss秒SSS毫秒 a";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "G YYYY年MM月dd日KK点mm分ss秒SSS毫秒 a";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "2016年第w星期";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "2016年12月第W星期";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "2016年第D天";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "2016年12月第d天";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "2016年12月31日处在2016年12月的第F星期";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "E";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "2016年12月31日是所在星期的第u天";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "大西洋标准时间:z";

System.out.println(new SimpleDateFormat(pattern).format(date));

pattern = "Z";

System.out.println("RFC822时区:" + new SimpleDateFormat(pattern).format(date));

pattern = "X";

System.out.println("ISO8601时区:" + new SimpleDateFormat(pattern).format(date));

}

运行结果:

Java中SimpleDateFormat的使用方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/silent_paladin/article/details/54646813

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java中SimpleDateFormat的使用方法 https://www.kuaiidc.com/113916.html

相关文章

发表评论
暂无评论