Coder Social home page Coder Social logo

Comments (1)

anidotnet avatar anidotnet commented on May 26, 2024

The behaviour you are facing is due to Calendar object's milliseconds field. As Nitrite stores date object as a long field, you need to clear the Calendar object first if you want to use it as a filter.

I have created a test for you which will clarify this.

Test Case 1: Your test case

@Test
    public void testDate() {
        ObjectRepository<HtmlContentModel> repository = db.getRepository(HtmlContentModel.class);

        for (int i = 0; i < 5; i++) {
            HtmlContentModel model = new HtmlContentModel();
            Calendar calendar= Calendar.getInstance();
            calendar.set(2017 - i,1,1,0,0,0);
            model.publishTime = calendar.getTime();
            repository.insert(model);
        }

        assertEquals(repository.size(), 5);

        for (HtmlContentModel model : repository.find()) {
            System.out.println(model);
        }

        System.out.println("**************************");

        Calendar calendar=Calendar.getInstance();
        calendar.set(2015,1,1,0,0,0);
        final Date startTime=calendar.getTime();
        System.out.println("startTime: " + startTime + " - " + startTime.getTime());
        calendar.set(2017,1,1,0,0,0);
        final Date endTime=calendar.getTime();
        System.out.println("endTime: " + endTime + " - " + endTime.getTime());

        System.out.println("**************************");

        ObjectFilter objectFilter=ObjectFilters.and(ObjectFilters.gte("publishTime",startTime), ObjectFilters.lt("publishTime",endTime));
        Cursor<HtmlContentModel> cursor=repository.find(objectFilter, FindOptions.sort("publishTime",SortOrder.Ascending));
        List<HtmlContentModel> list = cursor.toList();
        for (HtmlContentModel model : list) {
            System.out.println(model);
        }
    }

    public static class HtmlContentModel implements Serializable {
        public Date publishTime;

        @Override
        public String toString() {
            return "html { " + publishTime.toString() + " - " + publishTime.getTime() + " }";
        }
    }

Output:

html { Wed Feb 01 00:00:00 IST 2017 - 1485887400192 }
html { Mon Feb 01 00:00:00 IST 2016 - 1454265000256 }
html { Sun Feb 01 00:00:00 IST 2015 - 1422729000257 }
html { Sat Feb 01 00:00:00 IST 2014 - 1391193000258 }
html { Fri Feb 01 00:00:00 IST 2013 - 1359657000259 }
**************************
startTime: Sun Feb 01 00:00:00 IST 2015 - 1422729000315
endTime: Wed Feb 01 00:00:00 IST 2017 - 1485887400315
**************************
html { Mon Feb 01 00:00:00 IST 2016 - 1454265000256 }
html { Wed Feb 01 00:00:00 IST 2017 - 1485887400192 }

Process finished with exit code 0

Test Case 2: What you should do (clearing the Calendar object's milliseconds field)

@Test
    public void testDate() {
        ObjectRepository<HtmlContentModel> repository = db.getRepository(HtmlContentModel.class);

        for (int i = 0; i < 5; i++) {
            HtmlContentModel model = new HtmlContentModel();
            Calendar calendar= Calendar.getInstance();
            calendar.clear();
            calendar.set(2017 - i,1,1,0,0,0);
            model.publishTime = calendar.getTime();
            repository.insert(model);
        }

        assertEquals(repository.size(), 5);

        for (HtmlContentModel model : repository.find()) {
            System.out.println(model);
        }

        System.out.println("**************************");

        Calendar calendar=Calendar.getInstance();
        calendar.clear();
        calendar.set(2015,1,1,0,0,0);
        final Date startTime=calendar.getTime();
        System.out.println("startTime: " + startTime + " - " + startTime.getTime());
        calendar.clear();
        calendar.set(2017,1,1,0,0,0);
        final Date endTime=calendar.getTime();
        System.out.println("endTime: " + endTime + " - " + endTime.getTime());

        System.out.println("**************************");

        ObjectFilter objectFilter=ObjectFilters.and(ObjectFilters.gte("publishTime",startTime), ObjectFilters.lt("publishTime",endTime));
        Cursor<HtmlContentModel> cursor=repository.find(objectFilter, FindOptions.sort("publishTime",SortOrder.Ascending));
        List<HtmlContentModel> list = cursor.toList();
        for (HtmlContentModel model : list) {
            System.out.println(model);
        }
    }

    public static class HtmlContentModel implements Serializable {
        public Date publishTime;

        @Override
        public String toString() {
            return "html { " + publishTime.toString() + " - " + publishTime.getTime() + " }";
        }
    }

Output:

html { Wed Feb 01 00:00:00 IST 2017 - 1485887400000 }
html { Mon Feb 01 00:00:00 IST 2016 - 1454265000000 }
html { Sun Feb 01 00:00:00 IST 2015 - 1422729000000 }
html { Sat Feb 01 00:00:00 IST 2014 - 1391193000000 }
html { Fri Feb 01 00:00:00 IST 2013 - 1359657000000 }
**************************
startTime: Sun Feb 01 00:00:00 IST 2015 - 1422729000000
endTime: Wed Feb 01 00:00:00 IST 2017 - 1485887400000
**************************
html { Sun Feb 01 00:00:00 IST 2015 - 1422729000000 }
html { Mon Feb 01 00:00:00 IST 2016 - 1454265000000 }

Process finished with exit code 0

from nitrite-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.