StefiIOE

[NP] 4.4

Nov 12th, 2020
54
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.time.LocalDateTime;
  2. import java.time.ZoneId;
  3. import java.time.ZonedDateTime;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.Instant;
  6.  
  7. /**
  8.  * ZonedDateTime tests
  9.  */
  10. public class ZonedDateTimeTest {
  11.     public static void main(String[] args) {
  12.         System.out.println(zonedDateTimeOf());
  13.         System.out.println(zonedDateTimeParse());
  14.         System.out.println(zonedDateTimeFormat());
  15.         System.out.println(toPST());
  16.         System.out.println(sameInstantAs());
  17.         System.out.println(sameLocalAs());
  18.     }
  19.  
  20.     static ZonedDateTime zonedDateTimeOf() {
  21.         /**
  22.          * Create a {@link ZonedDateTime} with time of 2015-07-10 2:14:25.000 as Japan Standard Time
  23.          * by using {@link ZonedDateTime#of} and {@link ZoneId#of}
  24.          */
  25.        ZoneId id = ZoneId.of("Asia/Tokyo");
  26.        ZonedDateTime zdt = ZonedDateTime.of(2015, 07, 10, 2, 14, 25, 000000000, id);
  27.        return zdt;
  28.     }
  29.  
  30.     static ZonedDateTime zonedDateTimeParse() {
  31.         /**
  32.          * Create a {@link ZonedDateTime} with time of 2015-06-18 23:07:25.000 as Japan Standard Time
  33.          * by using {@link ZonedDateTime#parse}
  34.          */
  35.        
  36.      
  37.         ZonedDateTime zdt = ZonedDateTime.parse("2015-06-18T23:07:25+09:00[Asia/Tokyo]");
  38.         return zdt;
  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 format = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss_z");
  49.         String formatedDateTime = zdt.format(format);
  50.          return formatedDateTime;
  51.  
  52.            
  53.     }
  54.  
  55.     static ZonedDateTime toPST() {
  56.         LocalDateTime ldt = DateAndTimes.LDT_20150618_23073050;
  57.         /**
  58.          * Create a {@link ZonedDateTime} from {@link ldt} with Pacific Standard Time
  59.          */
  60.         ZoneId zoneId = ZoneId.of( "America/Los_Angeles" );
  61.         ZonedDateTime zdt = ldt.atZone( zoneId );
  62.         return zdt;
  63.     }
  64.  
  65.     static ZonedDateTime sameInstantAs() {
  66.         ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
  67.         /**
  68.          * Create a {@link ZonedDateTime} same instant as {@link zdt} with Pacific Standard Time
  69.          * by using {@link ZonedDateTime#withZoneSameInstant}
  70.          */
  71.  
  72.          ZonedDateTime zdt2 = zdt.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));
  73.  
  74.         return zdt2;
  75.     }
  76.  
  77.     static ZonedDateTime sameLocalAs() {
  78.         ZonedDateTime zdt = DateAndTimes.ZDT_20150618_23073050;
  79.         /**
  80.          * Create a {@link ZonedDateTime} same local time as {@link zdt} with Pacific Standard Time
  81.          * by using {@link ZonedDateTime#withZoneSameLocal}
  82.          */
  83.         ZonedDateTime zdt2 = zdt.withZoneSameLocal(ZoneId.of("America/Los_Angeles"));
  84.  
  85.         return zdt2;
  86.     }
  87.  
  88.     static class DateAndTimes {
  89.  
  90.         public static final LocalDateTime LDT_20150618_23073050 = LocalDateTime.of(2015, 6, 18, 23, 7, 30, 500000000);
  91.         public static final ZonedDateTime
  92.                 ZDT_20150618_23073050 = ZonedDateTime.of(LDT_20150618_23073050, ZoneId.of("Asia/Tokyo"));
  93.     }
  94. }
  95.  
RAW Paste Data