简体   繁体   English

使用 ObjectBox 查询当地时间

[英]Query Local Time with ObjectBox

I have two questions,我有两个问题,

  1. How do you save a LocalTime rage with ObjectBox, for example:如何使用 ObjectBox 保存 LocalTime 愤怒,例如:
class LocalTimeRage {
   LocalTime opens;
   LocalTime closes;
}

And 2. How can you query with ObjectBox a local time range, for example, query object that matches within opens and closes LocalTime values, for example, query for opens between 07:00 AM and closes 8:00 PM (in LocalTime) as such ObjectBox will return objects that have the opening and closing times matching the range.以及 2. 如何使用 ObjectBox 查询本地时间范围,例如,查询在openscloses LocalTime 值内匹配的 object,例如,查询在07:00 AM8:00 PM之间(在 LocalTime)关闭为这样的 ObjectBox 将返回具有与范围匹配的开始和结束时间的对象。

This is based on ObjectBox v2.8.1.这是基于 ObjectBox v2.8.1。

ObjectBox supports a relatively limited list of built-in types . ObjectBox 支持相对有限的内置类型列表。 The list does not include any of the modern java.time classes, but we can use the ObjectBox @Convert annotation .该列表不包括任何现代java.time类,但我们可以使用 ObjectBox @Convert注释

I think it is best to completely avoid the old and problematic Date class, so this example converts LocalDateTime values to longs (however, Date is one of their built-in supported types):我认为最好完全避免旧的和有问题Date class,所以这个例子将LocalDateTime值转换为 long(但是, Date是它们内置的支持类型之一):

import java.time.LocalTime;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Convert;
import io.objectbox.converter.PropertyConverter;

@Entity
public class LocalTimeRange {

    @Id
    private long id;

    @Convert(converter = LocalTimeConverter.class, dbType = Long.class)
    private LocalTime opens;

    @Convert(converter = LocalTimeConverter.class, dbType = Long.class)
    private LocalTime closes;

    public static class LocalTimeConverter implements PropertyConverter<LocalTime, Long> {

        @Override
        public LocalTime convertToEntityProperty(Long databaseValue) {
            if (databaseValue == null) {
                return null;
            }
            return LocalTime.ofSecondOfDay(databaseValue);
        }

        @Override
        public Long convertToDatabaseValue(LocalTime entityProperty) {
            if (entityProperty == null) {
                return null;
            }
            long seconds = (entityProperty.getHour() * 60 * 60) + 
                    (entityProperty.getMinute() * 60) + 
                    entityProperty.getSecond();
            return seconds;
        }

    }

    public LocalTimeRange(Long id) {
        this.id = id;
    }

    public LocalTimeRange(long id, LocalTime opens, LocalTime closes) {
        this.id = id;
        this.opens = opens;
        this.closes = closes;
    }

    public LocalTimeRange() {
    }
    
    // getters and setters not shown

}

Now we can create and store a couple of test objects:现在我们可以创建和存储几个测试对象:

BoxStore store = MyObjectBox.builder().name("objectbox-demo-db").build();

Box<LocalTimeRange> box = store.boxFor(LocalTimeRange.class);

// start with no objects:
box.query().build().remove();

// add two objects:
LocalTimeRange ltrOne = new LocalTimeRange(1, 
        LocalTime.of(9, 30, 0), //9:30:00 (9:30am)
        LocalTime.of(10, 15, 0));
box.put(ltrOne);

LocalTimeRange ltrTwo = new LocalTimeRange(2, 
        LocalTime.of(10, 05, 0),
        LocalTime.of(11, 45, 0));
box.put(ltrTwo);

And then we can query the data store:然后我们可以查询数据存储:

// this will find both objects:
LocalTime testTime = LocalTime.of(10, 10, 0);

// this will find the 2nd object
//LocalTime testTime = LocalTime.of(10, 20, 0);

// convert the localtime to seconds:
Long seconds = localTimeToSeconds(testTime);

List<LocalTimeRange> localTimeRanges = box.query()
        .less(LocalTimeRange_.opens, seconds)
        .greater(LocalTimeRange_.closes, seconds)
        .build().find();

for (LocalTimeRange ltr : localTimeRanges) {
    System.out.println(ltr.toString());
}

store.close();

---

private static long localTimeToSeconds(LocalTime lt) {
    return (lt.getHour() * 60 * 60) + 
                (lt.getMinute() * 60) + 
                lt.getSecond();
}

The query operators are also limited in the types they can handle:查询运算符也受限于它们可以处理的类型:

less(LocalTimeRange_.opens, seconds)

Here, seconds is a long - because there is no query operator which takes the Java type we want to use ( LocalTime ).在这里, secondslong - 因为没有查询运算符采用我们想要使用的 Java 类型( LocalTime )。

You may want to refine my example, to ensure that test times which fall exactly on one of the boundary values are handled correctly (there is no "less than or equal to" query method, for example).您可能想要改进我的示例,以确保正确处理恰好落在其中一个边界值上的测试时间(例如,没有“小于或等于”查询方法)。 You can build more sophisticated queries with equal() , and() , or() , and so on.您可以使用equal()and()or()等构建更复杂的查询。

(This approach handles LocalTime to the nearest second. If you want sub-second precision, you would need to update the logic to include LocalTime 's support for nanoseconds.) (此方法将LocalTime处理到最接近的秒数。如果您想要亚秒级精度,则需要更新逻辑以包括LocalTime对纳秒的支持。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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