Comments (1)
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)
- Kotlin helpers are non-nullable
- readme.md error HOT 1
- db corruption errors HOT 5
- why OkHttp? HOT 1
- DepShield Deprecation Notice
- NitriteBuilder#openOrCreate sometimes returns null HOT 10
- After upgrade 3.4.3 to 3.4.4 getting deserialization exceptions on db query HOT 1
- nitriteMapper is null? HOT 1
- Add 4.x.x+ to maven repository
- NitriteMapper does not give enough info about the type HOT 4
- IndexingException: indexing on arrays or collections for field formula are not supported HOT 1
- Longs become Integers after export/import HOT 7
- Migrating from 3.x to 4.x: Database file is corrupted HOT 7
- possible memory leak HOT 1
- org.h2.mvstore.Chunk.accountForRemovedPage NPE HOT 2
- MVStore claims to have unsaved changes after calling nitriteDatabase.getCollection(...) HOT 5
- NitriteCollection.update slow when unrelated properties in document are indexed HOT 6
- EntityConverter toDocument HOT 3
- KotlinXSerializationMapper decodes lists to be size 1 always HOT 1
- Invalid repository type for entity where all fields are nullable (with JacksonMapperModule) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nitrite-java.