/src/test/java/com/alibaba/json/bvt/jdk8/LocalDateTimeTest5.java

https://github.com/alibaba/fastjson · Java · 183 lines · 142 code · 40 blank · 1 comment · 0 complexity · 7d03eecb7d6264928a373e04ab976a7d MD5 · raw file

  1. package com.alibaba.json.bvt.jdk8;
  2. import java.time.Instant;
  3. import java.time.LocalDateTime;
  4. import java.util.Locale;
  5. import java.util.Random;
  6. import java.util.TimeZone;
  7. import com.alibaba.fastjson.JSON;
  8. import junit.framework.TestCase;
  9. public class LocalDateTimeTest5 extends TestCase {
  10. private static Random random = new Random();
  11. private Locale origin;
  12. private TimeZone original = TimeZone.getDefault();
  13. private String[] zoneIds = TimeZone.getAvailableIDs();
  14. @Override
  15. protected void setUp() throws Exception {
  16. int index = random.nextInt(zoneIds.length);
  17. TimeZone timeZone = TimeZone.getTimeZone(zoneIds[index]);
  18. TimeZone.setDefault(timeZone);
  19. JSON.defaultTimeZone = timeZone; // While running mvn tests defaultTimeZone might already be initialized
  20. origin = Locale.getDefault();
  21. }
  22. @Override
  23. protected void tearDown() throws Exception {
  24. TimeZone.setDefault(original);
  25. JSON.defaultTimeZone = original;
  26. Locale.setDefault(origin);
  27. }
  28. public void test_for_long() throws Exception {
  29. long millis = 1322874196000L;
  30. // using localDataTime instance so that different timeZones are tested
  31. LocalDateTime localDateTime = LocalDateTime.ofInstant(
  32. Instant.ofEpochMilli(millis), TimeZone.getDefault().toZoneId());
  33. VO vo = JSON.parseObject("{\"date\":" + millis + "}", VO.class);
  34. assertEquals("Not Matching year", localDateTime.getYear(), vo.date.getYear());
  35. assertEquals("Not Matching month", localDateTime.getMonthValue(), vo.date.getMonthValue());
  36. assertEquals("Not Matching day", localDateTime.getDayOfMonth(), vo.date.getDayOfMonth());
  37. assertEquals("Not Matching hour", localDateTime.getHour(), vo.date.getHour());
  38. assertEquals("Not Matching minute", localDateTime.getMinute(), vo.date.getMinute());
  39. assertEquals("Not Matching second", localDateTime.getSecond(), vo.date.getSecond());
  40. assertEquals("Not Matching nano", localDateTime.getNano(), vo.date.getNano());
  41. }
  42. public void test_for_normal() throws Exception {
  43. VO vo = JSON.parseObject("{\"date\":\"2011-12-03 09:03:16\"}", VO.class);
  44. assertEquals(2011, vo.date.getYear());
  45. assertEquals(12, vo.date.getMonthValue());
  46. assertEquals(3, vo.date.getDayOfMonth());
  47. assertEquals(9, vo.date.getHour());
  48. assertEquals(3, vo.date.getMinute());
  49. assertEquals(16, vo.date.getSecond());
  50. assertEquals(0, vo.date.getNano());
  51. }
  52. public void test_for_iso() throws Exception {
  53. VO vo = JSON.parseObject("{\"date\":\"2011-12-03T09:03:16\"}", VO.class);
  54. assertEquals(2011, vo.date.getYear());
  55. assertEquals(12, vo.date.getMonthValue());
  56. assertEquals(3, vo.date.getDayOfMonth());
  57. assertEquals(9, vo.date.getHour());
  58. assertEquals(3, vo.date.getMinute());
  59. assertEquals(16, vo.date.getSecond());
  60. assertEquals(0, vo.date.getNano());
  61. }
  62. public void test_for_tw() throws Exception {
  63. VO vo = JSON.parseObject("{\"date\":\"2016/05/06 09:03:16\"}", VO.class);
  64. assertEquals(2016, vo.date.getYear());
  65. assertEquals(5, vo.date.getMonthValue());
  66. assertEquals(6, vo.date.getDayOfMonth());
  67. }
  68. public void test_for_jp() throws Exception {
  69. VO vo = JSON.parseObject("{\"date\":\"2016年5月6日 09:03:16\"}", VO.class);
  70. assertEquals(2016, vo.date.getYear());
  71. assertEquals(5, vo.date.getMonthValue());
  72. assertEquals(6, vo.date.getDayOfMonth());
  73. }
  74. public void test_for_cn() throws Exception {
  75. VO vo = JSON.parseObject("{\"date\":\"2016年5月6日 9时3分16秒\"}", VO.class);
  76. assertEquals(2016, vo.date.getYear());
  77. assertEquals(5, vo.date.getMonthValue());
  78. assertEquals(6, vo.date.getDayOfMonth());
  79. }
  80. public void test_for_kr() throws Exception {
  81. VO vo = JSON.parseObject("{\"date\":\"2016년5월6일 09:03:16\"}", VO.class);
  82. assertEquals(2016, vo.date.getYear());
  83. assertEquals(5, vo.date.getMonthValue());
  84. assertEquals(6, vo.date.getDayOfMonth());
  85. }
  86. public void test_for_us() throws Exception {
  87. VO vo = JSON.parseObject("{\"date\":\"05/26/2016 09:03:16\"}", VO.class);
  88. assertEquals(2016, vo.date.getYear());
  89. assertEquals(5, vo.date.getMonthValue());
  90. assertEquals(26, vo.date.getDayOfMonth());
  91. }
  92. public void test_for_eur() throws Exception {
  93. VO vo = JSON.parseObject("{\"date\":\"26/05/2016 09:03:16\"}", VO.class);
  94. assertEquals(2016, vo.date.getYear());
  95. assertEquals(5, vo.date.getMonthValue());
  96. assertEquals(26, vo.date.getDayOfMonth());
  97. }
  98. public void test_for_us_1() throws Exception {
  99. Locale.setDefault(Locale.US);
  100. VO vo = JSON.parseObject("{\"date\":\"05/06/2016 09:03:16\"}", VO.class);
  101. assertEquals(2016, vo.date.getYear());
  102. assertEquals(5, vo.date.getMonthValue());
  103. assertEquals(06, vo.date.getDayOfMonth());
  104. }
  105. public void test_for_br() throws Exception {
  106. Locale.setDefault(new Locale("pt", "BR"));
  107. VO vo = JSON.parseObject("{\"date\":\"06/05/2016 09:03:16\"}", VO.class);
  108. assertEquals(2016, vo.date.getYear());
  109. assertEquals(5, vo.date.getMonthValue());
  110. assertEquals(6, vo.date.getDayOfMonth());
  111. }
  112. public void test_for_au() throws Exception {
  113. Locale.setDefault(new Locale("en", "AU"));
  114. VO vo = JSON.parseObject("{\"date\":\"06/05/2016 09:03:16\"}", VO.class);
  115. assertEquals(2016, vo.date.getYear());
  116. assertEquals(5, vo.date.getMonthValue());
  117. assertEquals(6, vo.date.getDayOfMonth());
  118. }
  119. public void test_for_de() throws Exception {
  120. Locale.setDefault(new Locale("pt", "BR"));
  121. VO vo = JSON.parseObject("{\"date\":\"06.05.2016 09:03:16\"}", VO.class);
  122. assertEquals(2016, vo.date.getYear());
  123. assertEquals(5, vo.date.getMonthValue());
  124. assertEquals(6, vo.date.getDayOfMonth());
  125. assertEquals(9, vo.date.getHour());
  126. assertEquals(3, vo.date.getMinute());
  127. assertEquals(16, vo.date.getSecond());
  128. assertEquals(0, vo.date.getNano());
  129. }
  130. public void test_for_in() throws Exception {
  131. VO vo = JSON.parseObject("{\"date\":\"06-05-2016 09:03:16\"}", VO.class);
  132. assertEquals(2016, vo.date.getYear());
  133. assertEquals(5, vo.date.getMonthValue());
  134. assertEquals(6, vo.date.getDayOfMonth());
  135. assertEquals(9, vo.date.getHour());
  136. assertEquals(3, vo.date.getMinute());
  137. assertEquals(16, vo.date.getSecond());
  138. assertEquals(0, vo.date.getNano());
  139. }
  140. public static class VO {
  141. public LocalDateTime date;
  142. }
  143. }