Date and DateTime problem. Date fields also store time in 00:00

791 views
Skip to first unread message

dan elebash

unread,
Oct 4, 2013, 10:41:59 AM10/4/13
to orient-...@googlegroups.com
Shouldn't a field of type Date only store date info with no Time even if Time is 00:00

My db config is 

dateFormat
 
 yyyy-MM-dd

dateTimeFormat   yyyy-MM-dd'T'HH:mm:ss

Example
Schema strict class Contact
Contact has field mydate of type date
Execute in OrientDb Studio Insert into Contact (mydate) values("2013-10-01")

Do a select Contact and the mydate field returns both date and time like this 2013-10-03T00:00:00

The main problem I am having is the when I do a Rest query my app expects a date to be in format yyyy-MM-dd and not DateTime format with time set to 0

An actual DateTime field is working correctly returning both date and time in format yyyy-MM-dd'T'HH:mm:ss

Thanks,

Dan

dan elebash

unread,
Oct 4, 2013, 8:23:22 PM10/4/13
to orient-...@googlegroups.com
Been digging in the code and as I understand it you are using java.util.date which basically is a long and does not care about date or datetime.  In the OJSONWriter class line 281 you check for Instance of Date  else if (iValue instanceof Date)  and then apply getDateTimeFormatInstance, but to me a Date field should be just a Date not a DateTime with utc of 00:00:00.

Since we have data types of datetime and date and different formats for date and datetime we need to apply the correct Format depending on if it is a Otype of Date or DateTime. 

Also I have been trying to figure out how to work around this, any ideas?  But having trouble getting field type from a Orecord Instance. 

dan elebash

unread,
Oct 4, 2013, 11:13:56 PM10/4/13
to orient-...@googlegroups.com
I don't really like touching the core of OrientDb but I was able to put a small patch in place that fixes writing field of type Dates when using .toJson function.
I hope that someone from the Dev team can confirm if fields of type Date really should store a time or not.  Hopefully not.  
This patch just checks for field of type Otype.Date and reformats it to the date format specified in Orient config before serializing to json

Note: this does not actually change the way Date fields store values they are still stored in like bleow where mydate is field type Date and mydatetime is field type DateTime
----+----+-------------------+-------------------
#   |@RID|mydate             |mydatetime
----+----+-------------------+-------------------
0   |#1:0|2014-10-18T00:00:00|2013-10-27T02:40:00
1   |#1:1|2013-10-06T00:00:00|2013-11-01T02:47:00
2   |#1:2|2013-10-04T00:00:00|2013-10-05T02:43:00
----+----+-------------------+-------------------

In my opinion the mydate field should not have T00:00:00 as that is a valid UTC time.

Attached is the patched class OJSONFetchListener
Just added a check for java.util.Date, then a helper function formatODate to check if that field is Otype.Date or Otype.DateTime and reformat.


Thanks,
Dan
OJSONFetchListener.java

Luca Garulli

unread,
Oct 5, 2013, 4:02:35 PM10/5/13
to orient-database
Hi,
the problem is that path is not caring about DATETIME case because in Java both DATE and DATETIME are managed by Date class.

Lvc@



--
 
---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

dan elebash

unread,
Oct 5, 2013, 10:05:49 PM10/5/13
to orient-...@googlegroups.com
Yes I understand that the Java Date class does not know if it is a Date or DateTime, but that to me is a problem at least when getting data from the server. Still experimenting with OrientDb.

But if you have a field of type Date ie Otype.Date then it should be just the Date without a time stamp or at least OrientDb should convert any fields of type Date to the formatDate string.  java.util.date does not know the difference but by declaring a field of type Date OrientDb does and should handle that appropriately.

For me this causes a problem when my client application expects a date string formated by OrientDb formatDate to be returned but instead gets a DateTime string formated by formatDateTime.

I have worked around this by the patch above, however if I just do an insert into a field of type Date via console the underlying date is stored as a DateTime not just Date, so I think that somehow the database should store Dates as just a Date.  I am new to java but the java.util.date class does not do any formatting of the Date it stores so as long as Orient checks that when it is adding data to any field of type Date it always formats it using the formatDate before storing it in the java.util.Date class then it should be ok.  At least from my basic understanding this is true.  Of course there are other java classes that manage dates and date times much better then the java.util.date that maybe we should use.  Like Date4J, Joda, ect or maybe java.sql.date and java.sql.timestamp. Again new to java so not exactly sure if what I am saying is correct, hope so.

Thanks,
Dan
Reply all
Reply to author
Forward
0 new messages