How to use Different time Zone in java 8 - ZoneId and ZonedDateTime






1) How to use Different timeZone in java 8 - ZoneId example



import java.time.ZoneId;
/**
* How to use Different time Zone in java 8
*/
public class TimeZoneExample {
   public static void main(String[] args) {
     
      System.out.println("--ZoneId.of  Asia/Kolkata--");
      ZoneId zoneId1 = ZoneId.of("Asia/Kolkata");
      System.out.println("zoneId1 = "+zoneId1); //zoneId1 = Asia/Kolkata
      System.out.println("zoneId1.getId() = "+zoneId1.getId());
      System.out.println("zoneId1.getRules() = "+zoneId1.getRules());
    
      System.out.println("\n--ZoneId.of  Europe/Paris--");
      ZoneId zoneId2 = ZoneId.of("Europe/Paris");
      System.out.println("zoneId2 = "+zoneId2); //zoneId2 = Europe/Paris
      System.out.println("zoneId2.getId() = "+zoneId2.getId());
      System.out.println("zoneId2.getRules() = "+zoneId2.getRules());
   }
}
/* OUTPUT
--ZoneId.of  Asia/Kolkata--
zoneId1 = Asia/Kolkata
zoneId1.getId() = Asia/Kolkata
zoneId1.getRules() = ZoneRules[currentStandardOffset=+05:30]
--ZoneId.of  Europe/Paris--
zoneId2 = Europe/Paris
zoneId2.getId() = Europe/Paris
zoneId2.getRules() = ZoneRules[currentStandardOffset=+01:00]
*/

2) How to use Different timeZone in java 8 - ZonedDateTime example
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
/**
* ZonedDateTime in java 8
*/
public class ZoneOffsetExample {
   public static void main(String[] args) {
          // Create ZonedDateTime
          // by passing zone Asia/Kolkata
          // in atZone method of LocalDateTime
          ZonedDateTime zonedDateTime = LocalDateTime.now().atZone(ZoneId.of("Asia/Kolkata"));
          // display ZonedDateTime
          System.out.println("ZonedDateTime = " + zonedDateTime);
          // Now, get ZoneOffset
          ZoneOffset zoneOffset = zonedDateTime.getOffset();
          // Now, display zone offset
          System.out.println("zoneOffset = " + zoneOffset); // +05:30
          System.out.println("zoneOffset.getId() = " + zoneOffset.getId()); // +05:30
   }
}
/* OUTPUT
ZonedDateTime = 2017-05-13T15:49:43.228+05:30[Asia/Kolkata]
zoneOffset = +05:30
zoneOffset.getId() = +05:30
*/

3) Display different timeZones in java 8
import java.time.ZoneId;
/**
* Display different timeZones in java 8
*/
public class DisplayFewDifferentTimeZoneExample {
   public static void main(String[] args) {
       //Display different timeZones
       System.out.println(ZoneId.of("US/Samoa").getRules()); //ZoneRules[currentStandardOffset=-11:00]
       System.out.println(ZoneId.of("CET").getRules()); //ZoneRules[currentStandardOffset=+01:00]
       System.out.println(ZoneId.of("Europe/Paris").getRules()); //ZoneRules[currentStandardOffset=+01:00]
       System.out.println(ZoneId.of("GMT").getRules()); //ZoneRules[currentStandardOffset=Z] // i.e. +00:00
       System.out.println(ZoneId.of("Asia/Kolkata").getRules()); //ZoneRules[currentStandardOffset=+05:30]
       System.out.println(ZoneId.of("Australia/Melbourne").getRules()); //ZoneRules[currentStandardOffset=+10:00]
  
   }
}
/*Output
ZoneRules[currentStandardOffset=-11:00]
ZoneRules[currentStandardOffset=+01:00]
ZoneRules[currentStandardOffset=+01:00]
ZoneRules[currentStandardOffset=Z]
ZoneRules[currentStandardOffset=+05:30]
ZoneRules[currentStandardOffset=+10:00]
*/

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.





eEdit
Must read for you :