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
More Conversion:
Convert ISO8601 to GMT Timestamp?
Convert GMT Timestamp to ISO8601 ?
Convet ZoneDateTime from ISO8601 ?
No comments:
Post a Comment