Advertisement
lameski

Untitled

Jan 25th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. import java.time.LocalDateTime;
  2. import java.time.ZoneId;
  3. import java.time.ZonedDateTime;
  4. import java.time.chrono.JapaneseChronology;
  5. import java.time.chrono.JapaneseDate;
  6. import java.time.chrono.JapaneseEra;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.format.DateTimeFormatterBuilder;
  9. import java.util.Locale;
  10. import java.util.TimeZone;
  11. /**
  12. * ZonedDateTime tests
  13. */
  14. public class ZonedDateTimeTest {
  15. public static void main(String[] args) {
  16. System.out.println(zonedDateTimeOf());
  17. System.out.println(zonedDateTimeParse());
  18. System.out.println(zonedDateTimeFormat());
  19. System.out.println(toPST());
  20. System.out.println(sameInstantAs());
  21. System.out.println(sameLocalAs());
  22. }
  23.  
  24. static ZonedDateTime zonedDateTimeOf() {
  25. /**
  26. * Create a {@link ZonedDateTime} with time of 2015-07-10 2:14:25.000 as Japan Standard Time
  27. * by using {@link ZonedDateTime#of} and {@link ZoneId#of}
  28. */
  29. return ZonedDateTime.of(2015, 7, 10, 2, 14, 25, 0, ZoneId.of("Asia/Tokyo"));
  30. }
  31.  
  32. static ZonedDateTime zonedDateTimeParse() {
  33. /**
  34. * Create a {@link ZonedDateTime} with time of 2015-06-18 23:07:25.000 as Japan Standard Time
  35. * by using {@link ZonedDateTime#parse}
  36. */
  37. return ZonedDateTime.parse("2015-06-18T23:07:25.000+09:00")
  38. .of(LocalDateTime.of(2015, 06, 18, 23, 7, 25), ZoneId.of("Asia/Tokyo"));
  39. }
  40.  
  41. static String zonedDateTimeFormat() {
  42. ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
  43.  
  44. /**
  45. * Format {@link zdt} to a {@link String} as "2015_06_18_23_07_30_JST"
  46. * by using {@link ZonedDateTime#format}
  47. */
  48. DateTimeFormatter a = DateTimeFormatter.ofPattern("2015_06_18_23_07_30_z")
  49. .withZone(ZoneId.of("Asia/Tokyo"));
  50. return zdt.format(a);
  51. //.withZone(ZoneId.of("Asia/Tokyo")));
  52. }
  53.  
  54. static ZonedDateTime toPST() {
  55. LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
  56. /**
  57. * Create a {@link ZonedDateTime} from {@link ldt} with Pacific Standard Time
  58. */
  59. return ZonedDateTime.of(ldt, ZoneId.of("America/Los_Angeles"));
  60. }
  61.  
  62. static ZonedDateTime sameInstantAs() {
  63. ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
  64. /**
  65. * Create a {@link ZonedDateTime} same instant as {@link zdt} with Pacific Standard Time
  66. * by using {@link ZonedDateTime#withZoneSameInstant}
  67. */
  68. return zdt.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
  69. }
  70.  
  71. static ZonedDateTime sameLocalAs() {
  72. ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
  73. /**
  74. * Create a {@link ZonedDateTime} same local time as {@link zdt} with Pacific Standard Time
  75. * by using {@link ZonedDateTime#withZoneSameLocal}
  76. */
  77. return zdt.withZoneSameLocal(ZoneId.of("America/Los_Angeles"));
  78. }
  79.  
  80. static class DateAndTimes {
  81.  
  82. public static final LocalDateTime LDT_20150618_23073050 = LocalDateTime.of(2015, 6, 18, 23, 7, 30, 500000000);
  83. public static final ZonedDateTime
  84. ZDT_20150618_23073050 = ZonedDateTime.of(LDT_20150618_23073050, ZoneId.of("Asia/Tokyo"));
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement