How to convert ZoneDateTime from ISO8601 ?




      TimeZone and Date Conversion using Java 8 APIs:

        ZoneDateTime from 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.convertZonedDateTimeFromIso8601();
                    
       }
      
       public void convertZonedDateTimeFromIso8601() {
              String currentTimeZone = "Asia/Tokyo";
              TimeZone timeZone = TimeZone.getTimeZone(currentTimeZone);
              ZoneId currentTimeZoneId = timeZone.toZoneId();
              String iso8601 = "2011-12-03T11:15:30+09:00";
              ZonedDateTime result = DatesTest.convertZonedDateTimeFromIso8601(iso8601, "Asia/Tokyo");
              System.out.println(" convertZonedDateTimeFromIso8601  :  "+result);
       }

}

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


public class ConvertDate {

      
       public static ZonedDateTime convertZonedDateTimeFromIso8601(String iso8061DateTime, String currentTimeZone) {
              ZonedDateTime zonedDateTime = null;
              if (iso8061DateTime != null) {
                     Boolean hasOffset = null;
                     LocalDateTime localDateTime = null;
                     try {
                           // Getting OffsetDateTime to check that provided
                           // ISO string has offset OR not.
                           OffsetDateTime offsetDateTime = OffsetDateTime.parse(iso8061DateTime);
                           System.out.println(" Check OFFSET : "+offsetDateTime.getOffset()); // eg.  +09:00 for Tokyo and +05:30 for India
                           hasOffset = Boolean.TRUE;
                     } catch (DateTimeParseException dte) {
                           try {
                                  hasOffset = Boolean.FALSE;
                                  localDateTime = LocalDateTime.parse(iso8061DateTime);
                           } catch (DateTimeParseException dte2) {
                           }
                     }
                     if (!hasOffset) {

                           if (currentTimeZone != null && !currentTimeZone.isEmpty()) {
                                  zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of(currentTimeZone));
                           }
                     } else {
                           zonedDateTime = ZonedDateTime.parse(iso8061DateTime);
                     }
              }
              return zonedDateTime;
       }
}

 ------------------------------------------------------------------------------

Output :


convertZonedDateTimeFromIso8601  :  2011-12-03T11:15:30+09:00




More Conversion:





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...