Android SQLite timestamp alteration - android

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')

Related

Android studio sqlite time 12:00 becomes 00:00

i am using datepicker inside my project and storing the date and time inside sql database. so my problem is that when i chose time: 12:13 and store it inside the database, i try to retrieve this time and i get 00:13 instead of 12:13.
Only 12:00 ose not work
This is my format that I use to store:
SimpleDateFormat test = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
And this is raw from my sql create table :
work_time datetime NOT NULL
As you see this is datatime type, so any suggestion how to fix this?
Humm... It seems that 00:00 means 12:00 in french time. This isn't a bug or problem, its the way its set.

Count all values of the last 60 minutes

I am currently working on a Project, where an Android App is saving some values with a timestamp in a SQLite Database. The timestamp is stored as type "DATE NOT NULL" in the format "yyyy-MM-dd HH:mm:ss".
(for example: ID: 1 | Timestamp: 2017-11-19 21:05:04 | Value: 1234).
Now, for some calculations, i have to choose the number of entries, which where created/stored in the last 60 minutes since the current time.
So i need something like:
SELECT Count(*) FROM TableName WHERE Timestamp > current_timestamp - 60 Minutes
Can you help me? One Problem is, that i cant change the way the Timestamp is saved.
I searched many topics which seem to be similar, but can't get it to work.
Thanks for reading and helping.
cold1ce
The stored timestamp seems legit for sqlite processing.
Just to be on the safe side you can try applying datetime() to it when using it in date calculations.
As regards your question, you have to do it approximately thus:
WHERE datetime(timestamp) > datetime(current_timestamp, '-60 minutes')

Sqlite date format in my app

I am storing date and time in sqlite as timestamp value e.g. 1486650099741 when i use it in my code using setTimeinMillis for GregorianCalendar it converts perfectly. However when i try to year for the same time (in millis) using strftime ('%Y',datetimestamp) in sqlite it gives me weird results (year is shown as 1698 and not 2017)
is it something to do with the epoch (used by sqlite) ?
Please try this select strftime('%Y', datetimestamp/ 1000, 'unixepoch');

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

datetime('now') gives wrong time

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

Categories

Resources