How to store Time in Android SQLite - android

From how to store 6:00pm in sqlite database in android, I know that for SQLite, there is no data type "TIME".
So, if I want to store time(year,month,day,hour,min,sec), how can I create that column? I should use "TEXT"?? But, how can I parse them for Android and
if I want to sort by time, how can I do it?
Thank for all your help!

You can define for yourself a date format (link) and you can create let's say a DateManager class to convert a date to a string (to store it in the SQLite database) and to convert a string into a date, both based on that date format specified by you.

The easiest solution would be to store the Date as miliseconds (long).
You can get the miliseconds from a date using date.getTime() when you store them, and when you re-create your Date object you could use Date d=new Date(milliseconds).

Related

How to do date conditions for sugar ORM queries

Been stuck trying to make this work for a while: I am using sugar ORM for sqlite in my android app and I am trying to write a query with a condition on a date field. The where clause format for sqlite dates does not seem to be working.
eg:
MySugarRecordClass.find(MySugarRecordClass.class, "some_date_field>?", "2015-01-01")
or
MySugarRecordClass.find(MySugarRecordClass.class, "date(some_date_field)>date(?)", "2015-01-01")
I have tried using several date formats but I still can't get it.
Unfortunetly SQLite (the DB behind Sugar) does not have a date type: http://www.sqlite.org/datatype3.html. I personally store and compare my dates as integers after converting them to the UNIXTIMESTAMP.
You can also store the date as a string to do a comparison on, but I suggest sticking with the 'YYYY-mm-dd hh:mm:ss' format in 24 hour format, like so: '2015-08-11 13:13:00' (time for 1:13pm exactly).
Whatever way you do it, you need to be consistent and save/compare your dates that exact way every time.

Compare Dates (stored as string) in android sqlite database

How to compare dates which stored as String in Sqlite Database and fetch data...?
I am storing date in a format 19-03-2014 14:23:43
I have tried BETWEEN and also >= and <= but none of them is working. And I want to compare only Date and not Time. So I am passing value as 20-03-2014, If time is necessary to pass then I don't have any problem to pass 00:00:00
String comparisons use the lexicographical ordering of characters.
To be able to compare date strings correctly, the most significant field must come first, i.e., you must use the format yyyy-MM-dd.
Set this date into date object and use before(obj) and after(obj) method of date object to compare two date.

Date into SQLite

in the TextView I have the date formatted in "Locale" now I have to insert it into the sqlite db. How do I convert the format suitable for SQLite?
This is my Content Values
cv.put(MyTable.DATE, mDate.getText().toString());
You can insert it as a long:
cv.put(MyTable.DATA, mDate.getTime());
Then retrieve it as a long and use the following constructor:
mDate = new Date(cv.getAsLong(MyTable.DATA));
Additionally, if you need to save the Locale, just store it separately as a String:
cv.put(MyTable.LOCALE, mLocale.getLanguage());
and get it later:
mLocale = new Locale(cv.getAsString(MyTable.LOCALE));
I suggest that you store strings in text format for machine-readable code. See
http://developer.android.com/reference/java/text/SimpleDateFormat.html. SimpleDateFormat allows you to go between Date and String. I've seen some documentation that suggests you should pass dates internally in binary format. I tend to disagree. Storing a date in text format (if you know the format) is simpler and safer.

SQLite sort by date

My date is in text format, as SQLite has no data type for date and time, so now I want to sort my data by date.
So when I query the database like
SELECT jobno, ondate FROM Reports ORDER BY DATE(ondate)
this will return the data sort alphabetically not by data. How would I sort by date?
Either use Unix-epoch format, which is basically an INTEGER type, or time string (YYYY-MM-DD stuff).
A list of time string formats you can use can be found here.
Sort unix-epoch as you would sort an INTEGER type, and a time string as you would sort a TEXT type.
When SQLite compares/sorts strings of the form dd/mm/YYYY, it handles them like any other string, i.e., the first characters in the string have priority, e.g., 01/10/2013 is sorted before 22/01/2000.
If you ever compare or sort dates, or use any of SQLite's built-in date functions, you must use one of the supported date formats, like YYYY-MM-DD.
(In you particular query, you are calling the DATE function, which returns NULL if it does not recognize the format of its parameter. This means that your records were not sorted at all.)
I would suggest 2 things, to put your dates in milliseconds or use a text with the format YYYYMMDD.
Convert them every time you want to display them or you want to store them.
It's not complicated, and it will save you a lot of trouble.
Storing the date as a timestamp i.e in millisecond is a better option.

Outputting specific rows from SQL database using Date format

I'm writing an application where users enter some details into an SQLite Database. I need to write code whereby the last 31 days of stored data in the database is outputted.
The code to save the date string is:
currentDateString = DateFormat.getDateInstance().format(new Date());
This gives the date in the format (for todays date) as 30 Mar 2012.
Is there anyway to change this data format into DD/MM/YYYY? (30/03/2012)
This string is saved into the SQL Database as follows:
ContentValues dbcv = new ContentValues();
dbcv.put(KEY_DATE, date);
(currentDateTime String = Date in previous class)
I wish to output every row of the database that comes within the last 31 days.
How can I do this?
Thanks!
First of all, you are doing two questions instead on just one, I will try to give you an answer to both.
To change the date format in java take a look at the documentation for the SimpleDateFormat, you can pass a pattern to the constructor of that class to make it format your dates the way you really want. In your specific case the pattern you are looking for is dd/MM/yyyy
If you are storing dates in your database as strings you lose some of the benefits that storing dates as they are have. For example, your second question will be easily performed if you were storing dates instead of strings in your tables since the only thing you need to achieve what you want is to do a select operation to your table using the date functions provided by SQLLite. Take a look a this webpage as it will give you an idea of what you can do with those functions: SQLLite Date Functions
Note: Of course, if you still want to store them as strings the only solution you will have is read all the records, parse all the strings back to dates and use the java.util.Calendar class to check which record is older than 31 days and which one is not. I'd rather do the SQL query instead of that as it has too much better performance (specially on a mobile device).
EDIT:
SQLLite and Android are not specifically my strengths, I did a small research and I found several posts in which people were actually storing them as strings as you initially started to do. If SQLLite on Android doesn't support storing dates as they are (as other databases do) then maybe the best option in your scenario is using a long value as the date. Look at the Date definition class, it has a method getTime that returns the date value as long value, you can use that value later to create a new instance of a date (using its constructor).
To format that date (the Date object, of course) in something readable by the user, DateFormat (or any of its implementations) is still the answer.
To do the query you want to do having dates stored as longs I suggest you to take a look to the DateUtils class from the Apache Commons Lang library. In that class you will find several functions that work on dates, so, before creating the query you need to fetch the rows in last 31 dates, you could use something like:
Date now = new Date();
Date before = DateUtils.addDays(now, -31);
long beforeAsLong = before.getTime();
String query = "select * from TABLE_NAME where DATE_FIELD >= " + beforeAsLong;
Hope this helps.

Categories

Resources