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');
Related
I use LocaleDate.now in Android to get data from Sqlite based on the start date of week and the end date of the week. For example, if start date is 20/07/2020, then the end date will be 26/07/2020. I will get data from range date betwwen 20/07/2020 and 26/07/2020. My code run fine until today, i get start date of this week is 27/07/2020, the end date is 02/08/2020. I surprised because i couldn't get the data from sql, so i debugged and i realized the Sqlite couldn't get the data from range date between 27/07/2020 and 02/08/2020. I think if the day of start date(27) > the day of the end date(2) then Sqlite can't query it. How to solve this problem?
SELECT name
FROM date_detail
where date BETWEEN '27/07/2020' and '02/08/2020'
//not show me anything
I think you can solve this problem by saving dates into database in another format, such as 'yyyy-mm-dd' or with timestamp, so it can select dates between two dates.
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.
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
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
I am using sqllite for android and I have a the completedDate in the db in the following format
2012-06-26 17:20
I am trying to get the all the rows of the Task table whose completed date is in the future (greater than now)
I do this
select * from Task where completedDate > datetime ('now')
The problem is that the comparison seems to be based on date only without the time stamp. I am getting all the rows with completed date that is starting from tomorrow. However if it is the date as today (regardless of the time) then I don't get the row
Why is that? Any pointet
I found the solution in case anyone is interested.
Create a String representation of current date and do the comparison with it like the following
Date d = new Date();
String dStr = ..... //convert d to string using using simple date formated
select * from Task where completedDate > dStr