Add Code for converting LocalDate to ISO 8601
This commit is contained in:
parent
4e24430ad2
commit
52c96522c1
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.localDateToISO;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
|
|
||||||
|
public class LocalDateToISO {
|
||||||
|
public String formatUsingDateTimeFormatter(LocalDate localDate) {
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX");
|
||||||
|
String formattedDate = localDate.atStartOfDay().atOffset(ZoneOffset.UTC).format(formatter);
|
||||||
|
return formattedDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String formatUsingSimpleDateFormat(LocalDate date) {
|
||||||
|
Date utilDate = Date.from(date.atStartOfDay(ZoneOffset.UTC).toInstant());
|
||||||
|
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
|
||||||
|
String formattedDate = dateFormat.format(utilDate);
|
||||||
|
return formattedDate;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user