How to convert ZoneDateTime to ISO8601 ?





      TimeZone and Date Conversion using Java 8 APIs:

       ZoneDateTime to ISO8601  :


-------------------------------- ConvertUtilTest.java-----------------------


import java.sql.Timestamp;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.TimeZone;

public class ConvertUtilTest {

       public static void main(String[] args) {
              ConvertUtilTest convertUtilTest = new ConvertUtilTest();

             
              convertUtilTest.convertZonedDateTimeToIso8601();

                           
       }
      
       public void convertZonedDateTimeToIso8601() {
              String currentTimeZone = "Asia/Shanghai";
              TimeZone timeZone = TimeZone.getTimeZone(currentTimeZone);
              // If CurrentTimeZone is provided invalid value then
              // TimeZone.getTimeZone automatically convert it to GMT timeZone
              ZoneId currentTimeZoneId = timeZone.toZoneId();
              ZonedDateTime zonedDateTime = ZonedDateTime.of(2017, 02, 06, 10, 55, 50, 0, currentTimeZoneId);
              String result = ConvertDate.convertZonedDateTimeToIso8601(zonedDateTime, "Asia/Tokyo");
              System.out.println(" convertZonedDateTimeToIso8601  :  "+result);
       }

}


--------------------------- ConvertDate.java---------------


public class ConvertDate {

      
       public static String convertZonedDateTimeToIso8601(ZonedDateTime zonedDateTime, String currentTimeZone) {
              String isoTime = null;
              if (zonedDateTime != null) {
                     TimeZone timeZone = TimeZone.getTimeZone(currentTimeZone);
                     ZoneId currentTimeZoneId = timeZone.toZoneId();
                    
                     if (currentTimeZone != null) {
                           isoTime = zonedDateTime.withZoneSameInstant(currentTimeZoneId).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
                     }
              }
              return isoTime;
       }
 }



Output :


convertZonedDateTimeToIso8601  :  2017-02-06T11:55:50+09:00








No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...