Add Code for converting LocalDate to ISO 8601

This commit is contained in:
anujgaud 2023-11-18 00:24:38 +05:30 committed by GitHub
parent 4e24430ad2
commit 52c96522c1
1 changed files with 25 additions and 0 deletions

View File

@ -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;
}
}