Can you convert a Timestamp to a FieldValue? - java

In my app, user will upload a document to the database under their subcollection, with a Timestamp attached to the document. In my Security Rules, I have it programmed so that the Timestamp will always be equal to FieldValue.serverTimestamp(). This saves a FieldValue to the database.
The problem is, whenever I get the documentSnapshot back from Firebase and access the field, it comes back as a Timestamp and not a FieldValue. Is there a way I can convert a Timestamp to a FieldValue, or vice-versa?

No, you really do want that Timestamp instead of a FieldValue. FieldValue tokens are only used for writing field values that need to have their actual values computed on the Firestore backend instead of on the client. They are just tokens, not actual values. When you fetch the document and read the value back out, it will have the actual Timestamp value (or whatever the final value is), which is what you need to work with.

Related

Firebase Real Time DB - DatabaseReference.push().getKey() is a TimeStamp

According to the Documentation, the below code can set a timestamp as the key of the node using push() in the Realtime Database.
public void uploadToDB(String s) {
databaseReference.push().setValue(s);
}
The returned key are below of my push(), as an example:
a) -MpfCu14jtIkEk28D3CB
b) -MpfCxv_Nzv3YJ87MfZH
My question is:
are they timestamp?
if yes, can I decode it back to a readable timestamp?
are they timestamp?
No, those pushed IDs are not timestamps. However, it contains a time component.
As Michael Lehenbauer mentioned in this blog article:
Push IDs are string identifiers that are generated client-side. They are a combination of a timestamp and some random bits. The timestamp ensures they are ordered chronologically, and the random bits ensure that each ID is unique, even if thousands of people are creating push IDs at the same time.
And to answer the second question:
if yes, can I decode it back to readable timestamp?
If you reverse the engineering, probably yes. Please check the following answer:
How are Firebase IDs generated?
But would not count on that. To have an order according to a time component, then you should add a property of type "timestamp", as explained in my answer from the following post:
How to save the current date/time when I add new value to Firebase Realtime Database

Firebase orderByChild on startAt() and endAt() return wrong results

I am searching record from Firebase Database on the formated date rang converted in String type. I am calling filtering query on requestPlaceDate.
Query query = ordersDatabaseRef.limitToFirst(1000).orderByChild(ConstanceFnc.requestPlacedDate).startAt(startDate).endAt(endDate);
query.addListenerForSingleValueEvent(orderListener);
Firebase return data including previous and next dates, not return specific date range data, what I am expecting from a query on startAt() and endAt()
I am searching record from Firebase database on the formated date rang converted in String type.
You aren't getting the desired results because your requestPlacedDate field holds a String and not a Timestamp. When you order String elements, the order is lexicographical. I answered a similar question, so please check my answer from the following post:
How to order the nodes in firebase console based on key
To accomplish the correct order for your results, you should change the type of your field to be Timestamp, as explained in my answer from the following post:
How to save the current date/time when I add new value to Firebase Realtime Database
Once your field will hold the Timestamp value, your Query will work perfectly fine.

Retrieving and Convert Oracle long data type in to java String with special characters

I'm trying to get long data type from an oracle database table and it is as follows.
But rs.getLong("cloumn") not supported and with rs.getString("cloumn") this retrieved as follows.
"\"\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\u003cp\u003efsdfs drgrgr gesegter\u003c/p\u003e\n\u003c/body\u003e\n\u003c/html\u003e\"
I want to retrieve the long data with special characters as mentioned earlier.
Please suggest me a method to get the correct value.
Database admin doesn't allow to change the data type. Therefore value should be correctly retrieve from long data type.

Storing session value in long type

I am storing id as a string in session in play framework with scala.
Redirect("/").withSession("userId" -> id.toString)
I want to know is it possible to store it in Long type, because every time when i have to use it i am converting it again into Long again.
Nope. Well the Http Header that you receive is an ascii string.
And hence on setting header or on receiving, you will manually have to convert your cookie pair (userId) to String and and back to Long respectively

Get a Date object out of Lucene Document

I have indexed a date in lucene using DateTools.dateToString to store the date in a particular field.
Is there any way to know if this was a date field, and more importantly how to get the date out again?
It's a fieldable with a long integer value.
Thanks
Lucene does not have strong-typing of fields, so the same field could have a date in one record and a string in another record, and a random integer in a third. It's up to your application to know what to look for in a particular field.
You can use the DateTools.StringToDate method to convert from a string back to a date.

Categories