Skip to content

Commit

Permalink
Updated LocalDateConversions
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 25, 2024
1 parent 69fd670 commit b6bc1c0
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -71,6 +72,10 @@ static LocalDate toLocalDate(Object fromInstance, Converter converter, Converter
return toZonedDateTime(fromInstance, options).toLocalDate();
}

static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalTime();
}

static BigDecimal toBigDecimal(Object fromInstance, Converter converter, ConverterOptions options) {
return BigDecimal.valueOf(toLong(fromInstance));
}
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.cedarsoftware.util.ClassUtilities;
import com.cedarsoftware.util.CollectionUtilities;
import com.cedarsoftware.util.DateUtilities;
import com.cedarsoftware.util.StringUtilities;

/**
* Instance conversion utility. Convert from primitive to other primitives, plus support for Number, Date,
Expand Down Expand Up @@ -229,7 +230,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(Double.class, Double.class), Converter::identity);
DEFAULT_FACTORY.put(pair(Boolean.class, Double.class), BooleanConversions::toDouble);
DEFAULT_FACTORY.put(pair(Character.class, Double.class), CharacterConversions::toDouble);
DEFAULT_FACTORY.put(pair(Instant.class, Double.class), InstantConversions::toLong);
DEFAULT_FACTORY.put(pair(Instant.class, Double.class), InstantConversions::toDouble);
DEFAULT_FACTORY.put(pair(LocalDate.class, Double.class), LocalDateConversions::toLong);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, Double.class), LocalDateTimeConversions::toLong);
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, Double.class), ZonedDateTimeConversions::toLong);
Expand Down Expand Up @@ -607,6 +608,36 @@ private static void buildFactoryConversions() {
return date.toInstant().atZone(options.getZoneId()).toLocalDateTime();
});

DEFAULT_FACTORY.put(pair(Void.class, LocalTime.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Long.class, LocalTime.class), NumberConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(Double.class, LocalTime.class), NumberConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(BigInteger.class, LocalTime.class), NumberConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(BigDecimal.class, LocalTime.class), NumberConversions::toLocalDateTime);
DEFAULT_FACTORY.put(pair(AtomicLong.class, LocalTime.class), NumberConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(java.sql.Date.class, LocalTime.class), DateConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(Timestamp.class, LocalTime.class), DateConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(Date.class, LocalTime.class), DateConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(Instant.class, LocalTime.class), InstantConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, LocalTime.class), LocalDateTimeConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(LocalDate.class, LocalTime.class), LocalDateConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(LocalTime.class, LocalTime.class), Converter::identity);
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, LocalTime.class), ZonedDateTimeConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(Calendar.class, LocalTime.class), CalendarConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(Number.class, LocalTime.class), NumberConversions::toLocalTime);
DEFAULT_FACTORY.put(pair(Map.class, LocalTime.class), (fromInstance, converter, options) -> {
Map<?, ?> map = (Map<?, ?>) fromInstance;
return converter.fromValueMap(map, LocalTime.class, null, options);
});
DEFAULT_FACTORY.put(pair(String.class, LocalTime.class), (fromInstance, converter, options) -> {
String str = StringUtilities.trimToEmpty((String)fromInstance);
Date date = DateUtilities.parseDate(str);
if (date == null) {
return null;
}
return converter.convert(date, LocalTime.class, options);
});


// ZonedDateTime conversions supported
DEFAULT_FACTORY.put(pair(Void.class, ZonedDateTime.class), VoidConversions::toNull);
DEFAULT_FACTORY.put(pair(Long.class, ZonedDateTime.class), NumberConversions::toZonedDateTime);
Expand All @@ -617,6 +648,7 @@ private static void buildFactoryConversions() {
DEFAULT_FACTORY.put(pair(java.sql.Date.class, ZonedDateTime.class), DateConversions::toZonedDateTime);
DEFAULT_FACTORY.put(pair(Timestamp.class, ZonedDateTime.class), DateConversions::toZonedDateTime);
DEFAULT_FACTORY.put(pair(Date.class, ZonedDateTime.class), DateConversions::toZonedDateTime);
DEFAULT_FACTORY.put(pair(Instant.class, ZonedDateTime.class), InstantConversions::toZonedDateTime);
DEFAULT_FACTORY.put(pair(LocalDate.class, ZonedDateTime.class), LocalDateConversions::toZonedDateTime);
DEFAULT_FACTORY.put(pair(LocalDateTime.class, ZonedDateTime.class), LocalDateTimeConversions::toZonedDateTime);
DEFAULT_FACTORY.put(pair(ZonedDateTime.class, ZonedDateTime.class), Converter::identity);
Expand All @@ -632,7 +664,7 @@ private static void buildFactoryConversions() {
if (date == null) {
return null;
}
return date.toInstant().atZone(options.getZoneId());
return converter.convert(date, ZonedDateTime.class, options);
});

// UUID conversions supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -88,6 +89,10 @@ static LocalDate toLocalDate(Object fromInstance, Converter converter, Converter
return toZonedDateTime(fromInstance, options).toLocalDate();
}

static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalTime();
}

static BigInteger toBigInteger(Object fromInstance, Converter converter, ConverterOptions options) {
return BigInteger.valueOf(toLong(fromInstance));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ static BigDecimal toBigDecimal(Object from, Converter converter, ConverterOption
return BigDecimal.valueOf(toLong(from));
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options);
}

static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options).toLocalDateTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicLong;

public class LocalDateConversions {

private static ZonedDateTime toZonedDateTime(Object fromInstance, ConverterOptions options) {
return ((LocalDate)fromInstance).atStartOfDay(options.getZoneId());
return ((LocalDate)fromInstance).atStartOfDay(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId());
}

static Instant toInstant(Object fromInstance, ConverterOptions options) {
Expand All @@ -29,6 +31,10 @@ static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, C
return toZonedDateTime(fromInstance, options).toLocalDateTime();
}

static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalTime();
}

static ZonedDateTime toZonedDateTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).withZoneSameInstant(options.getZoneId());
}
Expand Down Expand Up @@ -60,7 +66,10 @@ static Timestamp toTimestamp(Object fromInstance, Converter converter, Converter
}

static Calendar toCalendar(Object fromInstance, Converter converter, ConverterOptions options) {
return CalendarConversions.create(toLong(fromInstance, options), options);
ZonedDateTime time = toZonedDateTime(fromInstance, options);
GregorianCalendar calendar = new GregorianCalendar(options.getTimeZone());
calendar.setTimeInMillis(time.toInstant().toEpochMilli());
return calendar;
}

static java.sql.Date toSqlDate(Object fromInstance, Converter converter, ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -13,7 +15,7 @@

public class LocalDateTimeConversions {
private static ZonedDateTime toZonedDateTime(Object fromInstance, ConverterOptions options) {
return ((LocalDateTime)fromInstance).atZone(options.getSourceZoneIdForLocalDates());
return ((LocalDateTime)fromInstance).atZone(options.getSourceZoneIdForLocalDates()).withZoneSameInstant(options.getZoneId());
}

private static Instant toInstant(Object fromInstance, ConverterOptions options) {
Expand All @@ -25,7 +27,19 @@ private static long toLong(Object fromInstance, ConverterOptions options) {
}

static ZonedDateTime toZonedDateTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).withZoneSameInstant(options.getZoneId());
return toZonedDateTime(fromInstance, options);
}

static LocalDateTime toLocalDateTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalDateTime();
}

static LocalDate toLocalDate(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalDate();
}

static LocalTime toLocalTime(Object fromInstance, Converter converter, ConverterOptions options) {
return toZonedDateTime(fromInstance, options).toLocalTime();
}

static Instant toInstant(Object fromInstance, Converter converter, ConverterOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -187,8 +188,10 @@ static Date toDate(Object from, Converter converter, ConverterOptions options) {
return new Date(toLong(from));
}

static Instant toInstant(Object from) { return Instant.ofEpochMilli(toLong(from)); }

static Instant toInstant(Object from, Converter converter, ConverterOptions options) {
return Instant.ofEpochMilli(toLong(from));
return toInstant(from);
}

static java.sql.Date toSqlDate(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -204,14 +207,22 @@ static Calendar toCalendar(Object from, Converter converter, ConverterOptions op
}

static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions options) {
return Instant.ofEpochMilli(toLong(from)).atZone(options.getZoneId()).toLocalDate();
return toZonedDateTime(from, options).toLocalDate();
}

static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) {
return Instant.ofEpochMilli(toLong(from)).atZone(options.getZoneId()).toLocalDateTime();
return toZonedDateTime(from, options).toLocalDateTime();
}

static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) {
return toZonedDateTime(from, options).toLocalTime();
}

static ZonedDateTime toZonedDateTime(Object from, ConverterOptions options) {
return toInstant(from).atZone(options.getZoneId());
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter, ConverterOptions options) {
return Instant.ofEpochMilli(toLong(from)).atZone(options.getZoneId());
return toZonedDateTime(from, options);
}
}
Loading

0 comments on commit b6bc1c0

Please sign in to comment.