Where should I format date from a database before displaying it - Android - android-sqlite

I store the date in an SQLlite database as a long and want to view it in a ListView. At the moment I have the Database, a content provider, a loadermanager and a custom adapter. At the moment I'm using the custom adapter to format the data. However, there may be other possibilites, so I'm interested what is best practice.

An alternative, assuming that saving it as a long meets the recognised time string formats as per :-
Time Strings A time string can be in any of the following formats (e.g. the very last one):
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
SQL As Understood By SQLite - Date And Time Functions
would be to extract the data in the required display format.
For example consider the following :-
DROP TABLE IF EXISTS mydata;
CREATE TABLE IF NOT EXISTS mydata (mydatetime INTEGER);
INSERT INTO mydata VALUES(1092941466); -- <<<<<<<<<< 2004-08-19 18:51:06
SELECT *, datetime(mydatetime,'unixepoch'), strftime('%d/%m/%Y %H:%M:%S week %W of Year %Y',mydatetime,'unixepoch') FROM mydata;
The result would be :-
Reading the link above should indicate the flexibility of this approach.

Related

SQLite Convert to SQLite Datetime Format from ANY datetime format

Coming out of an Oracle background converting dates from any format to any format is really easy.
How is this done in SQLite? I've searched and searched for answers and most of the answers simply say... Save your date/strings in SQLite in one single format which is YYYY-MM-DD HH:MM:SS.SSS. This seems rigid to me.
I don't have that luxury as my data is stored in this format DD/MM/YYYY HH:MI:SS am ex. 3/7/2020 8:02:31 AM.
NOTE: For single days/months my date values do not contain leading zeros and my time is NOT in military time.
How do I tell SQLite what my date format is so that I can correctly convert my stored dates to SQLite datetime formats?
Convert from SQLite Date Format to Oracle Date Example:
In Oracle I would simply use the to_date function like so
to_date('2019-03-07 15:39:34', 'YYYY-MM-DD HH24:MI:SS')
All one needs to do is to tell the function what the date format is... and then it spits out a date.... easy peasy. What this example does is convert a SQLite date formated string to a date that Oracle recognizes as a date. It doesn't matter what the format is in as I tell the function what format to expect.
How do I Convert Dates in SQLite from any format to the SQLite Format?
Converting from SQLite's date format string to ANY date is easy as there are functions built in that do this easily... but how to do this the other way round?

Teradata - Date format-update query issues

There are two date columns in Teradata table with following definition.
Date1 DATE FORMAT 'DD-MM-YYYY'
Date2 DATE FORMAT 'DD-MM-YYYY'
As you can see, the format of the date is same for both the columns.
The values I see in Teradata SQL Assistant are different for Date1 & Date2 ...
For Date1 it is shows as MM/DD/YYYY
For Date2 it is shown as DD/MM/YYYY
Also, due to this, EXTRACT(MONTH FROM Date...) is not working same for both the columns.
Please note: Date2 column is updated by me with values like '04-28-2016' i.e. 'MM-DD-YYYY'. And this is to acheive the Date1 format as shown in SQL Assistant.
Can you please let me know where I made a mistake ?
Any advise would be helpful.
Thanks,
The FORMAT is used for casting from/to a string, but SQL Assistant uses the format specified under Tools -> Options -> Data Format -> Display dates in this format. And a DATE is stored in an internal format, thus EXTRACT is independent of the it.
Btw, the only recommended way to write a date is Standard SQL's DATE '2016-04-28'

Date Column Split in Talend

So I have one big file (13 million rows) and date formatted as:
2009-04-08T01:57:47Z. Now I would like to split it into 2 columns now,
one with just date as dd-MM-yyyy and other with time only hh:MM.
How do I do it?
You can simply use tMap and parseDate/formatDate to do what you want. It is neither necessary nor recommended to implement your own date parsing logic with regexes.
First of all, parse the timestamp using the format yyyy-MM-dd'T'HH:mm:ss'Z'. Then you can use the parsed Date to output the formatted date and time information you want:
dd-MM-yyyy for the date
HH:mm for the time (Note: you mixed up the case in your question, MM stands for the month)
If you put that logic into a tMap:
you will get the following:
Input:
timestamp 2009-04-08T01:57:47Z
Output:
date 08-04-2009
time 01:57
NOTE
Note that when you parse the timestamp with the mentioned format string (yyyy-MM-dd'T'HH:mm:ss'Z'), the time zone information is not parsed (having 'Z' as a literal). Since many applications do not properly set the time zone information anyway but always use 'Z' instead, so this can be safely ignored in most cases.
If you need proper time zone handling and by any chance are able to use Java 7, you may use yyyy-MM-dd'T'HH:mm:ssXXX instead to parse your timestamp.
I'm guessing Talend is falling over on the T and Z part of your date time stamp but this is easily resolved.
As your date time stamp is in a regular pattern we can easily extract the date and time from it with a tExtractRegexFields component.
You'll want to use "^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}):[0-9]{2}Z" as your regex which will capture the date in yyyy-MM-dd format and the time as mm:HH (you'll want to replace the date time field with a date field and a time field in the schema).
Then to format your date to your required format you'll want to use a tMap and use TalendDate.formatDate("dd-MM-yyyy",TalendDate.parseDate("yyyy-MM-dd",row7.date)) to return a string in the dd-MM-yyyy format.

How to change SQLite time format datetime

I am trying to get the date from SQLite. I am getting timestamp in coredata, but I need to see the date. What is the command to get the timestamp converted into YYYY-MM-DD format? My query is:
SELECT ZDATE FROM ZWEATHER
Zdate is datetime.
You may be looking for the DATE() function, as seen in the SQLite manual:
SELECT date(ZDATE);
The first thing to understand is that SQLite has no date type. It has no time type either.
It only offers some time functions. The page Piskvor links to is what you need. You enter the date and/or time in your database as a string following one of a few formats available:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
Or as a floating point value representing the Julian day number.

Saving Timestamp into sqlite

18.11.2009 10:32:00
I want the value in between the above tag(created) to be inserted into the sqlite db for which i have taken a column of type timestamp....
how can i store this value in that column??
please advise...
Well, it depends in which format you want to save it in your database.
If you want to save it in the string form, then save it directly by making an object. But, use the datatype of string type.
Another option is to save it using the date datatype, seeing as sqlite doesn't have a dedicated date/time datatype.
Use a formatter to set the date format:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-YYYY HH:MM:SS"];
Then make Date object, and save it.
You should replace dots by hyphens and place months, days and year parts in correct order.
According to sqlite docs these are the date and time accepted formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Two options here (if you want sorting, which I assume you do):
Add as a string, but re-order the fields from highest to lowest. After that, it's a simple matter to sort via text. This is the slower option.
2009.11.18 10:32:00
Convert to a UNIX timestamp, I.E. seconds since Jan 1st, 1970. This is the fastest option.
1261153920
It is then simple to pull it out using date and time functions.
Note that SQLite does not have a date/time data type.
Make object for date
then use this
[date timeIntervalSinceDate:objDate];
this give time interval beetween the date and objDate. two dates(date and objDate) for finding timeInterval.
Save this by convert it into string.