Convert LocalDateTime to Time Zone ISO8061 and vice versa in java 8






1) Convert LocalDateTime to Time Zone ISO8061 String in java 8
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class TimeZoneISO8061String_to_LocalDateTime {
   public static void main(String[] args) {
       //Convert LocalDateTime to Time Zone ISO8061 String
       System.out.println("Convert LocalDateTime to Time Zone ISO8061 String");
                                                 //Year,  Month,     dayOfMonth,   hour, minute, second
       LocalDateTime localDateTime2=LocalDateTime.of(2017, Month.JANUARY, 22,       19,  55, 56);
       //Use different zone
       ZonedDateTime zonedDateTime = localDateTime2.atZone(ZoneOffset.UTC);
       String str1 = zonedDateTime.toString();
       System.out.println("String with ISO8061 = " + str1); // 2017-01-22T19:55:56Z
   }
}
/*Output
Convert LocalDateTime to Time Zone ISO8061 String
String with ISO8061 = 2017-01-22T19:55:56Z
*/


2)  Convert ISO8061 String back to a LocalDateTime in java 8
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
public class LocalDateTime_To_TimeZoneISO8061String {
   public static void main(String[] args) {
       //Convert ISO8061 String back to a LocalDateTime
       System.out.println("Convert ISO8061 String back to a LocalDateTime");
       String str = "2017-01-22T19:55:56Z";
       ZonedDateTime zonedDateTime = ZonedDateTime.parse(str);
       LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
       System.out.println("localDateTime = " + localDateTime);
   }
}
/*Output
Convert ISO8061 String back to a LocalDateTime
localDateTime = 2017-01-22T19:55:56
*/



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 :