Martina312

Untitled

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