Skip to content

Commit

Permalink
Add support for java.sql.Date and java.sql.Timestamp when writing to …
Browse files Browse the repository at this point in the history
…excel
  • Loading branch information
agrancaric committed Jan 24, 2024
1 parent cb1ee62 commit 050579e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -39,8 +40,9 @@ public final class TypeDataFormatUtil {
private TypeDataFormatUtil() {
}

public static List<TypeDataFormat> resolveTypeDataFormatList(String dateFormat, String dateTimeFormat, String integerNumberFormat, String decimalNumberFormat, boolean writeDateWithTime,
List<TypeDataFormat> additionalTypeDataFormatList) {
public static List<TypeDataFormat> resolveTypeDataFormatList(
String dateFormat, String dateTimeFormat, String integerNumberFormat, String decimalNumberFormat, boolean writeDateWithTime, List<TypeDataFormat> additionalTypeDataFormatList
) {
String resolvedDateTimeFormat = writeDateWithTime ? dateTimeFormat : dateFormat;

List<TypeDataFormat> nonNullAdditionalDataFormatList = Optional.ofNullable(additionalTypeDataFormatList).orElse(Collections.emptyList());
Expand All @@ -49,9 +51,11 @@ public static List<TypeDataFormat> resolveTypeDataFormatList(String dateFormat,
new TypeDataFormat(Date.class, dateFormat),
new TypeDataFormat(Instant.class, dateFormat),
new TypeDataFormat(LocalDate.class, dateFormat),
new TypeDataFormat(java.sql.Date.class, dateFormat),
new TypeDataFormat(LocalDateTime.class, resolvedDateTimeFormat),
new TypeDataFormat(ZonedDateTime.class, resolvedDateTimeFormat),
new TypeDataFormat(OffsetDateTime.class, resolvedDateTimeFormat),
new TypeDataFormat(Timestamp.class, resolvedDateTimeFormat),
new TypeDataFormat(Short.class, integerNumberFormat),
new TypeDataFormat(Integer.class, integerNumberFormat),
new TypeDataFormat(Long.class, integerNumberFormat),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.context.support.ResourceBundleMessageSource;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -70,6 +71,7 @@ private static Stream<Arguments> shouldReturnTrueIfConversionIsSupportedMethodSo
arguments(LocalDateTime.now(), true),
arguments(OffsetDateTime.now(), true),
arguments(ZonedDateTime.now(), true),
arguments(new Timestamp(System.currentTimeMillis()), true),
arguments((short) 1, true),
arguments(1, true),
arguments(1L, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -48,11 +49,11 @@ void shouldReturnTypeDataFormatList() {

// then
assertThat(formatList).extracting("type").containsExactly(
Date.class, Instant.class, LocalDate.class, LocalDateTime.class, ZonedDateTime.class, OffsetDateTime.class,
Date.class, Instant.class, LocalDate.class, java.sql.Date.class, LocalDateTime.class, ZonedDateTime.class, OffsetDateTime.class, Timestamp.class,
Short.class, Integer.class, Long.class, BigInteger.class, Float.class, Double.class, BigDecimal.class
);
assertThat(formatList).extracting("dataFormat").containsExactly(
dateFormat, dateFormat, dateFormat, dateTimeFormat, dateTimeFormat, dateTimeFormat,
dateFormat, dateFormat, dateFormat, dateFormat, dateTimeFormat, dateTimeFormat, dateTimeFormat, dateTimeFormat,
integerFormat, integerFormat, integerFormat, integerFormat, decimalFormat, decimalFormat, decimalFormat
);
}
Expand All @@ -78,15 +79,16 @@ void shouldReturnDateFormatWithTimeWhenEnabled() {
@Test
void shouldAllowForAdditionalFormatsToBeSpecified() {
// given
String dateFormat = "dd/MM/yyyy";
List<TypeDataFormat> overriddenFormatList = Collections.singletonList(new TypeDataFormat(java.sql.Date.class, dateFormat));
Class<?> additionalClass = Object.class;
String dataFormat = "dd/MM/yyyy";
List<TypeDataFormat> overriddenFormatList = Collections.singletonList(new TypeDataFormat(additionalClass, dataFormat));

// when
List<TypeDataFormat> formatList = TypeDataFormatUtil.resolveTypeDataFormatList("dd.MM.yyyy.", "dd.MM.yyyy. HH:mm", "#,##0", "#,##0.00", true, overriddenFormatList);
TypeDataFormat dateTypeDataFormat = formatList.stream().filter(typeDataFormat -> java.sql.Date.class.equals(typeDataFormat.getType())).findFirst().orElse(null);
TypeDataFormat dateTypeDataFormat = formatList.stream().filter(typeDataFormat -> additionalClass.equals(typeDataFormat.getType())).findFirst().orElse(null);

// then
assertThat(dateTypeDataFormat).isNotNull();
assertThat(dateTypeDataFormat.getDataFormat()).isEqualTo(dateFormat);
assertThat(dateTypeDataFormat.getDataFormat()).isEqualTo(dataFormat);
}
}

0 comments on commit 050579e

Please sign in to comment.