i am trying to use compareTo method based on firebase docs to get how long ago was the database entry created.
https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp
I am trying to do something like this
var postedAgo = myEntry.dateCreated.compareTo(firebase.database.ServerValue.TIMESTAMP);
myEntry.dateCreated is stored TIMESTAMP, so all i got in my db is numerical value. I hope thats right. But the problem is when i log this it says that compareTo is not a function.
I am obviously doing something wrong, but i cant find almost anything on compareTo beyond the documentation. I wonder if anyone is even using it.
Thanks
Luke
The Firebase Database ServerValue.TIMESTAMP is not an actual value of a timestamp, but a so-called marker value that the server recognized (and then replaces by the current timestamp when writing to the database). Because of this, you cannot use ServerValue.TIMESTAMP in pure client-side operations.
If you want to determine how long ago a node was created, you'd take the timestamp from that node and subtract it from the current timestamp. So if myEntry.dateCreated is a timestamp that was written with ServerValue.TIMESTAMP, you could do:
System.currentTimeMillis() - myEntry.dateCreated
Related
I want to store a long value as string for various reasons so im trying to convert serverValue.TIMESTAMP to string but not able to. Can someone helpe me out?
Code I tried
mDatabaseReference.child("Chats").child(MessageSenderId).child(MessageRecieverId)
.child("Seen").setValue(String.valueOf(ServerValue.TIMESTAMP));
Result
"{.sv=timestamp}"
The database is storing the value like that and i have no idea why. I just can't store it as a long value, so I need to convert it to string because while recieving all the values should be String due to some feature I have. So can someone help me in converting this long to string value?
The ServerValue.TIMESTAMP is a so-called marker that you send to the database server. The database server then expands that into the actual timestamp. As you can see in your string version, the marker is actually a map.
There is no way to instruct the database to store the ServerValue.TIMESTAMP value as a string. Your options are:
Modify the feature to deal with the actual numeric value.
Store the client-side timestamp as a string from your app, with setValue(String.valueOf(System.currentTimeMillis()))
Write a Cloud Function that triggers on the server-side timestamp and then converts it to a string.
I want to add a timestamp of a data entry creation, example:
myRef.child("uid").setValue(data);
//add timestamp to the same path myRef.child("uid").setValue(<timestamp here>)
What would be the best timestamp, so that it will be independent of any time zone? (For example if a user's phone clock is set to the wrong time)
I did see this article from Firebae docs: Timestamp, but can't figure how to use it.
Use the value placeholder firebase.database.ServerValue.TIMESTAMP to update a value with the current clock time on the server at the time of the write, as seen by the server. It's a token value that means nothing on the client but has a special meaning on the server when it's written.
inside Firebase Functions transform the timestamp like so:
timestampObj.toDate()
timestampObj.toMillis().toString()
documentation here https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp
I'm working with an Android App that functions as an instant messaging service, and I'm at the point of pulling the message data through into a RecyclerView. I have my Firebase Query object returning all the messages that match the id of the chat that has been loaded, this is done like so:
Query qChatMessages = mDbRefMessages
.orderByChild("messageChat")
.equalTo(mChatId);
So far so good, and this is returning the collection of messages that I expect. The problem is now that, upon passing them to the RecyclerView to be displayed, they come out in the inverse order of how you would typically expect an instant messenger to display, so the messages are actually getting older as you scroll down the chat.
My message nodes in the database all have a messageTimestamp field associated with them, so this is what I would want to sort the data by. The problem is that I don't seem to be able to find a way of ordering the data by any field other than the one that I'm querying on, or getting the results back from Firebase and subsequently ordering them.
Does anyone know how I can work around this, or if there's some Firebase capabilities I'm not aware of?
Thanks in advance,
Mark
The easiest way is to store an extra piece of data in your message nodes which is the epoch time multiplied by -1: this way you can sort on this field and the query will return the right order.
See this post on how to get the epoch time: how to get the number of seconds passed since 1970 for a date value?
However, note that with your current data structure, you will encounter a problem: you will need to sort on this new data (e.g. with .orderByChild("messageCreationTime")) AND filter for a given mChatId (e.g. with equalTo(mChatId)), which is not possible.
So, you would have to re-structure your database (if this is possible) like this:
- messages
- mChatId
- messageUniqueID <- most probably auto-generated by Firebase
- messageTitle: ....
- messageCreationTime: -1525857044
And you would query by:
Query qChatMessages = databaseReference.child("messages").child(mChatId)
.orderByChild("messageCreationTime");
Hey please pay attention to solve this problem I know this could be possible but I am not getting the way ..
I am using sqlite in android to store and manipulate the field as shown below in the figure
Figure http://s16.postimage.org/idc13pxdh/image.png
first_start_time,last_start_time,last_end_time are datetime field in SQLite database
I need to get the difference of last_start_time and last_end_time as mean of function so that I don't need to do it programatically
last_end_time = strftime('%Y-%m-%d %H:%M:%S','now','localtime')
last_start_time= strftime('%Y-%m-%d %H:%M:%S','2012-01-27 02:34:56')
,i,e if my firsttime is =2012-06-15 14:54:33
and Endtime is =2012-06-15 14:54:40
then time diff would be like this =00:00:14;
i want to have such difference time like last_end_time -last_start_time
i also want to use trigger to do use in sqlite data base in android
at the last I need that all my total_expended_time should get summed up like ...
select sum(total_expended_time) from study_result
.. one thing keep in mind the result should be in datetime stamp so that i can reflect it to my UI
Any suggestion will be appreciated
I'm trying to persistently store time data. I write the time to the preferences as a string passing it the time.toString(), and then restore it from the string by using the time.parse(String) method. However, I find that the parse method is throwing a TimeFormatException, specifically:
android.util.TimeFormatException: Unexpected character 0x41 at pos=15. Expected Z
I use logcat to view the string i am passing to parse, and it looks normal:
20110321T021030America/Detroit(1,79,-14400,1,1300687830)
Can anyone figure out why this is? Does the "expected Z" mean the letter Z specifically, or does it mean any integer, or what? And why is this happening? It seems like parsing a Time's toString() would be the easiest way to ensure that there ISN'T a timeformatexception, and yet I am still getting one.
It probably just doesn't recognise the format. You could use time.getTime() to get the unix time value instead, this might be easier to use.
The problem with parsing the date format which has been passed to time.parse(); function
Please refer the link to rectify you problem
Custom Date and Time Format Strings