Reputation: 5709
I have a database where a date is represented as a long (BIGINT). I want to fetch the data into a record CheckIn
, like the following
context.select(CHECKINS.CHECKIN_TIME,
CHECKINS.CHECKOUT_TIME
).from(CHECKINS)
.fetch(Records.mapping(CheckIn::new));
The two columns in CHECKINS are represented as TableField<CheckinsRecord, Long>
. CheckIn
is defined as
public record CheckIn(LocalDateTime checkIn, LocalDateTime checkOut) {
}
Can I convert CHECKINS.CHECKIN_TIME
to a LocalDateTime right in the select, or do I need to do the conversion later?
Upvotes: 0
Views: 86
Reputation:
you can use the java.time.ZoneId
class to specify a time zone for the LocalDateTime
object, like this:
context.select(CHECKINS.CHECKIN_TIME,
CHECKINS.CHECKOUT_TIME
).from(CHECKINS)
.fetch(r -> new CheckIn(
Instant.ofEpochMilli(r.get(CHECKINS.CHECKIN_TIME)).atZone(ZoneId.of("UTC")).toLocalDateTime(),
Instant.ofEpochMilli(r.get(CHECKINS.CHECKOUT_TIME)).atZone(ZoneId.of("UTC")).toLocalDateTime()
));
Upvotes: 2