微信登录

formatter python

  1. package com.atguigu.java;
  2. import org.junit.Test;
  3. import java.time.*;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.format.FormatStyle;
  6. import java.time.temporal.TemporalAccessor;
  7. import java.util.Date;
  8. /**
  9. * jdk 8中日期时间API的测试
  10. *
  11. * @author shkstart
  12. * @create 2019 下午 2:44
  13. */
  14. public class JDK8DateTimeTest {
  15. @Test
  16. public void testDate(){
  17. //偏移量
  18. Date date1 = new Date(2020 - 1900,9 - 1,8);
  19. System.out.println(date1);//Tue Sep 08 00:00:00 GMT+08:00 2020
  20. }
  21. /*
  22. LocalDate、LocalTime、LocalDateTime 的使用
  23. 说明:
  24. 1.LocalDateTime相较于LocalDate、LocalTime,使用频率要高
  25. 2.类似于Calendar
  26. */
  27. @Test
  28. public void test1(){
  29. //now():获取当前的日期、时间、日期+时间
  30. LocalDate localDate = LocalDate.now();
  31. LocalTime localTime = LocalTime.now();
  32. LocalDateTime localDateTime = LocalDateTime.now();
  33. System.out.println(localDate);
  34. System.out.println(localTime);
  35. System.out.println(localDateTime);
  36. //of():设置指定的年、月、日、时、分、秒。没有偏移量
  37. LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
  38. System.out.println(localDateTime1);
  39. //getXxx():获取相关的属性
  40. System.out.println(localDateTime.getDayOfMonth());
  41. System.out.println(localDateTime.getDayOfWeek());
  42. System.out.println(localDateTime.getMonth());
  43. System.out.println(localDateTime.getMonthValue());
  44. System.out.println(localDateTime.getMinute());
  45. //体现不可变性
  46. //withXxx():设置相关的属性
  47. LocalDate localDate1 = localDate.withDayOfMonth(22);
  48. System.out.println(localDate);
  49. System.out.println(localDate1);
  50. LocalDateTime localDateTime2 = localDateTime.withHour(4);
  51. System.out.println(localDateTime);
  52. System.out.println(localDateTime2);
  53. //不可变性
  54. LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
  55. System.out.println(localDateTime);
  56. System.out.println(localDateTime3);
  57. LocalDateTime localDateTime4 = localDateTime.minusDays(6);
  58. System.out.println(localDateTime);
  59. System.out.println(localDateTime4);
  60. }
  61. /*
  62. Instant的使用
  63. 类似于 java.util.Date类
  64. */
  65. @Test
  66. public void test2(){
  67. //now():获取本初子午线对应的标准时间
  68. Instant instant = Instant.now();
  69. System.out.println(instant);//2019-02-18T07:29:41.719Z
  70. //添加时间的偏移量
  71. OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
  72. System.out.println(offsetDateTime);//2019-02-18T15:32:50.611+08:00
  73. //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
  74. long milli = instant.toEpochMilli();
  75. System.out.println(milli);
  76. //ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
  77. Instant instant1 = Instant.ofEpochMilli(1550475314878L);
  78. System.out.println(instant1);
  79. }
  80. /*
  81. DateTimeFormatter:格式化或解析日期、时间
  82. 类似于SimpleDateFormat
  83. */
  84. @Test
  85. public void test3(){
  86. // 方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
  87. DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
  88. //格式化:日期-->字符串
  89. LocalDateTime localDateTime = LocalDateTime.now();
  90. String str1 = formatter.format(localDateTime);
  91. System.out.println(localDateTime);
  92. System.out.println(str1);//2019-02-18T15:42:18.797
  93. //解析:字符串 -->日期
  94. TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797");
  95. System.out.println(parse);
  96. // 方式二:
  97. // 本地化相关的格式。如:ofLocalizedDateTime()
  98. // FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
  99. DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
  100. //格式化
  101. String str2 = formatter1.format(localDateTime);
  102. System.out.println(str2);//2019年2月18日 下午03时47分16秒
  103. // 本地化相关的格式。如:ofLocalizedDate()
  104. // FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
  105. DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
  106. //格式化
  107. String str3 = formatter2.format(LocalDate.now());
  108. System.out.println(str3);//2019-2-18
  109. // 重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
  110. DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
  111. //格式化
  112. String str4 = formatter3.format(LocalDateTime.now());
  113. System.out.println(str4);//2019-02-18 03:52:09
  114. //解析
  115. TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09");
  116. System.out.println(accessor);
  117. }
  118. }