Skip to content

Commit

Permalink
- Added Currency and Pattern to Converter.
Browse files Browse the repository at this point in the history
- All Date-time types can be converted to Year, YearMonth, and MonthDay
- java.sql.Date is treated as a true "date" with no surprises, like a birth date.
  • Loading branch information
jdereg committed Feb 8, 2025
1 parent 157df54 commit 0895dfc
Show file tree
Hide file tree
Showing 44 changed files with 2,323 additions and 1,062 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ static Date toDate(Object from, Converter converter) {
}

static java.sql.Date toSqlDate(Object from, Converter converter) {
return new java.sql.Date(toInstant(from, converter).toEpochMilli());
Instant instant = toInstant(from, converter);
// Convert the Instant to a LocalDate using the converter's zoneId.
LocalDate ld = instant.atZone(converter.getOptions().getZoneId()).toLocalDate();
// Return a java.sql.Date that represents that LocalDate (normalized to midnight).
return java.sql.Date.valueOf(ld);
}

static Timestamp toTimestamp(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ static Date toDate(Object from, Converter converter) {
}

static java.sql.Date toSqlDate(Object from, Converter converter) {
return new java.sql.Date(toInstant(from, converter).toEpochMilli());
BigInteger nanos = (BigInteger) from;
return java.sql.Date.valueOf(
Instant.ofEpochMilli(nanos.divide(MILLION).longValue())
.atZone(converter.getOptions().getZoneId())
.toLocalDate()
);
}

static Timestamp toTimestamp(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -44,46 +47,66 @@ final class CalendarConversions {

private CalendarConversions() {}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
Calendar calendar = (Calendar)from;
return calendar.toInstant().atZone(calendar.getTimeZone().toZoneId());
}

static Long toLong(Object from, Converter converter) {
return ((Calendar) from).getTime().getTime();
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
return new AtomicLong(((Calendar) from).getTime().getTime());
}

static double toDouble(Object from, Converter converter) {
Calendar calendar = (Calendar) from;
long epochMillis = calendar.getTime().getTime();
return epochMillis / 1000.0;
}


static BigDecimal toBigDecimal(Object from, Converter converter) {
Calendar cal = (Calendar) from;
long epochMillis = cal.getTime().getTime();
return new BigDecimal(epochMillis).divide(BigDecimalConversions.GRAND);
}

static BigInteger toBigInteger(Object from, Converter converter) {
return BigInteger.valueOf(((Calendar) from).getTime().getTime() * 1_000_000L);
}

static Date toDate(Object from, Converter converter) {
return ((Calendar) from).getTime();
}

static java.sql.Date toSqlDate(Object from, Converter converter) {
return new java.sql.Date(((Calendar) from).getTime().getTime());
return java.sql.Date.valueOf(
((Calendar) from).toInstant()
.atZone(converter.getOptions().getZoneId())
.toLocalDate()
);
}

static Timestamp toTimestamp(Object from, Converter converter) {
return new Timestamp(((Calendar) from).getTimeInMillis());
}

static AtomicLong toAtomicLong(Object from, Converter converter) {
return new AtomicLong(((Calendar) from).getTime().getTime());
}

static Instant toInstant(Object from, Converter converter) {
Calendar calendar = (Calendar) from;
return calendar.toInstant();
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
Calendar calendar = (Calendar)from;
return calendar.toInstant().atZone(calendar.getTimeZone().toZoneId());
}

static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDateTime();
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
Calendar cal = (Calendar) from;
OffsetDateTime offsetDateTime = cal.toInstant().atOffset(ZoneOffset.ofTotalSeconds(cal.getTimeZone().getOffset(cal.getTimeInMillis()) / 1000));
return offsetDateTime;
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDate();
}
Expand All @@ -92,16 +115,6 @@ static LocalTime toLocalTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalTime();
}

static BigDecimal toBigDecimal(Object from, Converter converter) {
Calendar cal = (Calendar) from;
long epochMillis = cal.getTime().getTime();
return new BigDecimal(epochMillis).divide(BigDecimalConversions.GRAND);
}

static BigInteger toBigInteger(Object from, Converter converter) {
return BigInteger.valueOf(((Calendar) from).getTime().getTime() * 1_000_000L);
}

static Calendar clone(Object from, Converter converter) {
Calendar calendar = (Calendar)from;
// mutable class, so clone it.
Expand All @@ -115,6 +128,30 @@ static Calendar create(long epochMilli, Converter converter) {
return cal;
}

static Year toYear(Object from, Converter converter) {
return Year.from(
((Calendar) from).toInstant()
.atZone(converter.getOptions().getZoneId())
.toLocalDate()
);
}

static YearMonth toYearMonth(Object from, Converter converter) {
return YearMonth.from(
((Calendar) from).toInstant()
.atZone(converter.getOptions().getZoneId())
.toLocalDate()
);
}

static MonthDay toMonthDay(Object from, Converter converter) {
return MonthDay.from(
((Calendar) from).toInstant()
.atZone(converter.getOptions().getZoneId())
.toLocalDate()
);
}

static String toString(Object from, Converter converter) {
ZonedDateTime zdt = toZonedDateTime(from, converter);
String zoneId = zdt.getZone().getId();
Expand Down Expand Up @@ -148,15 +185,9 @@ static String toString(Object from, Converter converter) {
}
}

static OffsetDateTime toOffsetDateTime(Object from, Converter converter) {
Calendar cal = (Calendar) from;
OffsetDateTime offsetDateTime = cal.toInstant().atOffset(ZoneOffset.ofTotalSeconds(cal.getTimeZone().getOffset(cal.getTimeInMillis()) / 1000));
return offsetDateTime;
}

static Map<String, Object> toMap(Object from, Converter converter) {
Map<String, Object> target = new LinkedHashMap<>();
target.put(MapConversions.CALENDAR, toString(from, converter));
return target;
}
}
}
Loading

0 comments on commit 0895dfc

Please sign in to comment.