homaxto
homaxto

Reputation: 5709

How do I convert a long to a LocalDateTime with jOOQ

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

Answers (1)

user19145409
user19145409

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

Related Questions