I want to know how can I get the value of the timeStamp from the ServerValue.TIMESTAMP, directly from the HashMap? I know that when that value is sent to Firebase I get the long value, but I want to get that long before send it to Firebase.
Okie so after some research I found that we cannot use ServerValue.TIMESTAMP before sending it to database because
ServerValue.TIMESTAMP is set as a Map (containing {.sv: "timestamp"}) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long.
Link
By the way you can use android own timestamp and store it in Firebase.
Calendar cc = Calendar.getInstance();
Date date = cc.getTime();
// SimpleDateFormat format1 = new SimpleDateFormat("dd MMM");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
timestamp = format2.format(date);
Related
I am writing to you to see if someone can guide me what is happening with this code ...
I have the following code:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
Date datofecha1 = new Date (time1);
String datotexto1 = formatter.format(datofecha1);
System.out.println(datotexto1);
the long of time1, I am fetching it from my cloud firebase database, it is an item number 1597081737457
That code should print me:
I/System.out: 10.08.2020, 13:48:57
but it prints me
I/System.out: 31.12.1969, 20:00:00
IF I change the long in my database, it gives me another date to print, it always prints December 31, 1969, a total madness (whatever the length I put on it).
As a curious fact if I put the long direct to the code in this way:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
Date datofecha1 = new Date (1597081737457L);
String datotexto1 = formatter.format(datofecha1);
System.out.println(datotexto1);
In the following way if the date prints me perfect
As a curious fact in the database, I can only put a number for the long, I can't put the final L, which must be put for the long, and I'm really stuck trying to print the date of the long used, and not that date of December 31 that always puts me.
Data stored in a Firebase is retrieved by attaching an asynchronous listener. You have to use value inside listener or ensure the listener is triggered before
I have an SQLite database in an Android project with a Date column that stores the date-time as String in dd-mm-yyyy HH-mm-ss format. I need to sort it based on the descending order of the date.
Or, convert it to the standard yyyy-MM-dd HH-mm-ss format and then sort it.
The general ORDER BY DATETIME(coulmn_name) doesnt work.
NOTE:
This is not a duplicate question, other answers advice to change the database schema (Which is not possible, because I have data stored already)
I would like to suggest an alternative approach to the one you are taking. I personally ran into the same issue and solved it by not using a string date at all.
Instead i converted the date to epoch milliseconds ie unix timestamp and saved that. Then a sort is a simple order by the timestamp.
You can use the following approach:
SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss");
Date currentDate = sdf.parse(yourdatestring);
//Get the calendar in the time zone you need, generally it works off the bat with the default time zone
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("yourtz"));
cal.setTime(currentDate);
//Get the milliseconds since epoch time
long millis = cal.getTimeInMillis();
You can save this timestamp and easily sort it. It'll be more accurate and easy to use than a string and potentially gives you the ability to handle different time zones.
You can retrieve the date by setting this timestamp directly in the calendar and getting a date from it
cal.setTimeInMillis(timestamp).getTime();
Hope this helps
How do i convert ServerValue.TIMESTAMP into SimpleDateFormat("dd MM yyyy")
Date is deprecated so not able to use it , is their is any way to use calender
You cannot use Server.TIMESTAMP to get a date. The doc says:
A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) as determined by the Firebase servers
This means that when you setValue() or updateChildren(), you can put this constant in the Map to tell the server to put the epoch time in that node instead. For example:
mRef = FirebaseDatabase.getInstance().child("whatever/path/in/your/database");
mRef.setValue( Server.TIMESTAMP );
This will set in <your Firebase>/whatever/path/in/your/database a long that will look like 149141530600. This is the current epoch time I fetched while writing this answer. It corresponds to the number of milliseconds passed since january 1st 1970 to when I copied the value. Then, if you have a listener to that node, you can get the calendar using:
Long time = dataSnapshot.getValue(Long.class);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(time);
If you only want the time the server is set to (saving it in the database is pointless), you can use the special node:
`FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset");`
A listener to this node returns a Double that represents an approximative offset between the device time and the server time. You can then set the Calendar using:
calendar.setTimeInMillis(System.currentTimeMillis() + offset);
You can use the following.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ServerValue.TIMESTAMP);
SimpleDateFormat fmt = new SimpleDateFormat("dd MM yyyy",Locale.US);
fmt.format(cal.getTime()); //This returns a string formatted in the above way.
If ServerValue.TIMESTAMP is returned as a string, you can parse the string using Long.parseLong(Server.TIMESTAMP);
Hello I am developing an application using parse.When I save an object there are some fields updated autamatically like createdAt and updatedAt which saves the date and time at which the object was created or updated.When I am creating or updating a parse object I get the date and time not the date of my computer and my computer is having a correct date and time still the createdAt and updatedAt fields are not containing the accurate values.
Parse use GMT+00 timezone for Date fields
All dates stored on Parse are stored in UTC. so make sure you convert them to correct time zone
Date p =pObject.getCreatedAt();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
p = sdf.parse(sdf.format(p));
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(sdf.format(p));
I am returning data from a sqlite3 database the some else wrote. I am trying to retrieve the date and time. To store it into the db I converted the date and time into one int. using this line
int currentTime=(int) ((newTime).toMillis(true) / 1000);
I am able to retrieve the data as an int, but cannot figure out how to convert the number back into a
date and time. Currently the db returns int 13333380180, I am trying to convert it to today's date and time.
long yourmilliseconds = 13333380180;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate));
Use the above code to convert
The number you're receiving is the number of milliseconds that have passed since January 1st, 1970. It's a standard, consistent way of representing a date & time on a number of platforms.
Although the value is an integer in SQLite, SQLite integers can be up to 64 bits. So, the value being returned to you should be treated as a long in Java. You can convert it to a Java Date object in a straightforward fashion: new Date(currentTime).