How to convert GMT Timestamp to ISO8601 ?





      TimeZone and Date Conversion using Java 8 APIs:

        GMT Timestamp 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.convertGmtTimestampToIso8601();
             
       }
       public void convertGmtTimestampToIso8601() {

              // Parameters:year the year minus 1900month 0 to 11date 1 to 31hour 0 to
              // 23minute 0 to 59second 0 to 59nano 0 to 999,999,999

              Timestamp testTimestamp = new Timestamp(111, 11, 3, 2, 15, 30, 0);
String result = ConvertDate.convertGmtTimestampToIso8601(testTimestamp, "Asia/Tokyo");
              System.out.println(" convertGmtTimestampToIso8601 :  " + result);
       }
}



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


public class ConvertDate {

       public static String convertGmtTimestampToIso8601(Timestamp gmtTimestamp, String currentTimeZone) {

              String isoTime = null;
              if (gmtTimestamp != null) {
                     LocalDateTime gmtLocalDateTime = gmtTimestamp.toLocalDateTime();
                     TimeZone timeZone = TimeZone.getTimeZone(currentTimeZone);
                     ZoneId currentTimeZoneId = timeZone.toZoneId();
                     if (currentTimeZone != null) {
                           ZonedDateTime gmtZonedDateTime = ZonedDateTime.of(gmtLocalDateTime, ZoneId.of("GMT"));
                           isoTime = gmtZonedDateTime.withZoneSameInstant(currentTimeZoneId).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
                     }
              }
              return isoTime;
       }
}


Output :


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