datetime('now') gives wrong time - android

The System-Time of my Android emulator is correct (currently 13:42). But when i use the datetime('now')-function to set the current time in my SQLite Database, the returned value is wrong (11:42).
Is there another time i need to set to get this working correctly?

The return value of datetime('now') is in UTC.
Try
datetime('now', 'localtime')

datetime('now') will return in GMT UTC - which you probably should do then handle the conversion to your local timezone in the app. If you keep it in UTC in the database, then convert it in your activities, your app will work correctly as the user moves around timezones

Related

Check whether specific time has surpassed using Firebase timestamp

I am stuck on a problem in Firebase where I have would like to:
Check the date and time using the Firebase.ServerValue.TIMESTAMP.
Increment the value of a variable when the date changes.
I have understood how to capture the time and date but how can I know whether the time is 2:00PM of 6 Feb has surpassed and change the value value of the variable after that. Any help would be appreciated.
Convert the date into milliseconds for both for comparison then simply perform a magnitude check.

Sqlite: select ('now') not works as expected

I want to get the current date as yyyy-MM-dd from SQLite, I use the following query:
***SELECT date('now')***
But instead of returning the current date, it returns the next day from today.
For example, today (2015-12-01) I run the query and it returns (2015-12-02).
What I did wrong?
Image running query + calendar
sqlite date and time functions use UTC time zone internally. In UTC the date was already 2015-12-02.
If you want to use another timezone, you need to specify it explicitly, e.g.
select date('now','-05:00');
I'd suggest to use UTC millisecond timestamps in your database layer though and have the presentation logic such as date formatting with timezone adjustment in your app code.
Reference: https://www.sqlite.org/lang_datefunc.html

Date difference is showing negative value in android

In sqlite date difference is coming negative.
SELECT (strftime('%s','now') - strftime('%s', msl_last_sync_date_time))/60/60 as last_synch_time
FROM mobi_sync_log
WHERE my msl_last_sync_date_time is 2015-06-24 10:36:27
Please suggest some idea.
now returns a time in UTC. 2015-06-24 10:36:27 is in the future in UTC considering when you posted the question.
Consider storing all your datetimes in UTC, or add an explicit timezone such as 2015-06-24 10:36:27 +05:30 to them.

Last Time Contacted

How does android calculate the value for last time contacted. It provides the value in integer format but I am unable to decipher the given value. For example what if I want to compare the two give values to know which contact has been contacted later. Any type of help would be appreciated.
The value you receive is returned in long format and holds the date and time information of the last time called has happened from a particular number.
It returns the milliseconds of that number.
For your reference: http://developer.android.com/reference/android/provider/ContactsContract.ContactOptionsColumns.html#LAST_TIME_CONTACTED
Now how to compute date and time here is the link:
how to convert milliseconds to date format in android?
How to compare:
There are functions "before" and "after" which tells whether the event date is before or after another specified date
Date class: http://developer.android.com/reference/java/util/Date.html#after(java.util.Date)
or
Calendar class: http://developer.android.com/reference/java/util/Calendar.html#after(java.lang.Object)
Depends which one you want to use.
I hope it helps and let me know for more info.
Every time you contact someone, (i.e. make the call Contacts.markAsContacted) the TIMES_CONTACTED should increment by 1 and LAST_TIME_CONTACTED should be updated. However, many device manufacturers have changed this functionality and it is not reliable anymore. There is an open bug for this issue which you could find at
http://code.google.com/p/android/issues/detail?id=8784&q=LAST_TIME_CONTACTED&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

Android SQLite timestamp alteration

I'm saving the timestamp of a user action to a SQLite Database. It all works fine except the time stamp is in GMT. I need to change it to +x hours ahead, but I'm having trouble formatting the SQL command:
myDB.execSQL("INSERT INTO "+ DATE_TABLE+" VALUES(null,'"+spt.toString()+"',datetime())" );
datetime() function gives the GMT time/date stamp. AS shown here http://www.sqlite.org/lang_datefunc.html
I tried datetime('localtime') which gives a value of NULL.
I tried datetime('+x hour') with hour and hours which returned NULL.
I want to know how to alter the time stamp for localtime?
Try these:
datetime('now','localtime')
datetime('now','+4 hours')

Categories

Resources