简体   繁体   中英

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?

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()
        ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM