diff --git a/core-java-modules/core-java-8-datetime-2/src/main/java/com/baeldung/localdatetoiso/LocalDateToISO.java b/core-java-modules/core-java-8-datetime-2/src/main/java/com/baeldung/localdatetoiso/LocalDateToISO.java index a04d554d5a..a62be336ca 100644 --- a/core-java-modules/core-java-8-datetime-2/src/main/java/com/baeldung/localdatetoiso/LocalDateToISO.java +++ b/core-java-modules/core-java-8-datetime-2/src/main/java/com/baeldung/localdatetoiso/LocalDateToISO.java @@ -14,6 +14,8 @@ import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.ISODateTimeFormat; +import org.apache.commons.lang3.time.FastDateFormat; + public class LocalDateToISO { public String formatUsingDateTimeFormatter(LocalDate localDate) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"); @@ -32,4 +34,11 @@ public class LocalDateToISO { org.joda.time.format.DateTimeFormatter formatter = ISODateTimeFormat.dateTime(); return formatter.print(localDate.toDateTimeAtStartOfDay(DateTimeZone.UTC)); } + + public String formatUsingApacheCommonsLang(LocalDate localDate) { + Date date = Date.from(localDate.atStartOfDay().toInstant(ZoneOffset.UTC)); + String formattedDate = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", TimeZone.getTimeZone("UTC")) + .format(date); + return formattedDate; + } }