Merge pull request #15236 from anujgaud/ag/localdate-to-iso8601

LocalDate to ISO 8601 conversion
This commit is contained in:
davidmartinezbarua 2023-12-15 14:17:57 -03:00 committed by GitHub
commit b04c7afcf5
2 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,44 @@
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;
import org.joda.time.DateTime;
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");
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:ss.SSSX");
String formattedDate = dateFormat.format(utilDate);
return formattedDate;
}
public String formatUsingJodaTime(org.joda.time.LocalDate localDate) {
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;
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.localdatetoiso;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.time.LocalDate;
public class LocalDateToISOUnitTest {
@Test
void givenLocalDate_whenUsingDateTimeFormatter_thenISOFormat(){
LocalDateToISO localDateToISO = new LocalDateToISO();
LocalDate localDate = LocalDate.of(2023, 11, 6);
String expected = "2023-11-06T00:00:00.000Z";
String actual = localDateToISO.formatUsingDateTimeFormatter(localDate);
assertEquals(expected, actual);
}
@Test
void givenLocalDate_whenUsingSimpleDateFormat_thenISOFormat(){
LocalDateToISO localDateToISO = new LocalDateToISO();
LocalDate localDate = LocalDate.of(2023, 11, 6);
String expected = "2023-11-06T00:00:00.000Z";
String actual = localDateToISO.formatUsingSimpleDateFormat(localDate);
assertEquals(expected, actual);
}
@Test
void givenLocalDate_whenUsingJodaTime_thenISOFormat() {
LocalDateToISO localDateToISO = new LocalDateToISO();
org.joda.time.LocalDate localDate = new org.joda.time.LocalDate(2023, 11, 6);
String expected = "2023-11-06T00:00:00.000Z";
String actual = localDateToISO.formatUsingJodaTime(localDate);
assertEquals(expected, actual);
}
@Test
void givenLocalDate_whenUsingApacheCommonsLang_thenISOFormat() {
LocalDateToISO localDateToISO = new LocalDateToISO();
LocalDate localDate = LocalDate.of(2023, 11, 6);
String expected = "2023-11-06T00:00:00.000Z";
String actual = localDateToISO.formatUsingApacheCommonsLang(localDate);
assertEquals(expected, actual);
}
}