Compare Dates Stored as String using Datetime - android

I have stored dates' date as String in my database in the following format :
dateFormater = new SimpleDateFormat("dd MMM yyyy");
Now i need to access data between the two dates startWeekDate and endWeekDate both formatted as above. Currently , the where clause of my query is :
at_date BETWEEN Datetime( '"+startWeekDate+"') AND Datetime( '"+endWeekDate+"')
Where startWeekDate is '10 Aug 2014' and endWeekDate is '16 Aug 2014'. But I am getting the null result on my TextView. Can any one guide me how can i do this.

This is not one of the supported date formats.
Just use yyyy-MM-dd.

Related

Datetime comparison in SQLite with unique format

I have a requirement, wherein column having date time information must be compared against present date & time. If earlier then this particular record must be deleted.
I have progressed with below implementation, but not getting any error & expected behavior is not met.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
Calendar nowCalendar = Calendar.getInstance();
String strNowTime = dateFormat.format(nowCalendar.getTime());
// Delete the records at Manager_Dashboard SQLiteDB
String whereClause = "DATETIME(End_Time) < DATETIME(?)";
String whereArgs[] = {strNowTime};
dbOfflineRange.delete("Offline_Range",whereClause,whereArgs);
As you could notice, if I have a record with End_Time in text within SQLite table (as 30 Nov 2018 21:35), then this should be compared against current datetime (as 30 Nov 2018 23:35)
As End_Time being earlier than Now, I am expecting deletion. But its not happening.
The Date and Time SQL functions require the date to be in a recognised format.
Time Strings A time string can be in any of the following 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
SQL As Understood By SQLite - Date And Time Functions
Your best(simplest) option would be to store them as YYYY-MM-DD HH:MM (i.e. change the SimpleDateFormat accordingly).
Note you would not even need the datetime function as direct comparison of the values would result in the desired outcome.
Storing the unix timestamp value would be the more efficient storage/processing time wise.
You can then use the strftime function to return the date in the format you wish.
Otherwise you can convert the store value to a recognised format using SQL functions such as substr and then do the comparison.
SQL As Understood By SQLite - Core Functions
here's an example of how you could handle data stored in DD MMM YYYY HH:MM format
assuming that the value to compare against is provided in YYYYMMDDHH:MM format :-
DROP TABLE IF EXISTS Offline_Range;
CREATE TABLE IF NOT EXISTS Offline_Range (end_time);
-- Add some test data dd MMM yyyy HH:mm
INSERT INTO Offline_Range VALUES
('21 May 2018 10:30'),
('21 Jun 2018 10:30'),
('21 Jul 2018 10:30'),
('21 Aug 2018 10:30')
;
SELECT * FROM Offline_Range
WHERE
substr(end_time,8,4)||
CASE
WHEN instr(end_time,'Jan') THEN '01'
WHEN instr(end_time,'Feb') THEN '02'
WHEN instr(end_time,'Mar') THEN '03'
WHEN instr(end_time,'Apr') THEN '04'
WHEN instr(end_time,'May') THEN '05'
WHEN instr(end_time,'Jun') THEN '06'
WHEN instr(end_time,'Jul') THEN '07'
WHEN instr(end_time,'Aug') THEN '08'
WHEN instr(end_time,'Sep') THEN '09'
WHEN instr(end_time,'Oct') THEN '10'
WHEN instr(end_time,'Nov') THEN '11'
WHEN instr(end_time,'Dec') THEN '12'
END
||
substr(end_time,1,2)||substr(14,5)
<
'2018062200:00' -- 22 Jun 2018 00:00 amended to suit i.e. the value input
This creates a table :-
The result from the query would be :-

ORMLite Timestamp recovery error

I'm running ORMlite on Android app. I have created a #DatabaseTable class with a Timestamp field.
#DatabaseTable
public class Cliente {
#DatabaseField
private Timestamp dateinicio;
}
I can insert items but when I try to recover data I get this error:
java.sql.SQLException: Could not assign object 'Wed Jan 29 13:35:01 CET 2014'
to field FieldType:name=dateinicio,class=Cliente
I suppose this must be a typical error but I cannot resolve it.
Is this existing data you are trying to get to work or did ORMLite store this date for you? Either way you are going to have to match the formats that Sqlite supports.
Internally ORMLite uses "yyyy-MM-dd HH:mm:ss.SSSSSS". You can specify the format using something like the following where the ... is replaced with the format which matches your data.
#DatabaseField(format = "...")
private Timestamp dateinicio;
Sqlite officially supports the following date 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
See this answer here: ORMLite query date format issue

How to change string to datetime format in android?

I am working on android project. I am setting date and time but it is displaying in the following format.
Mon Nov 19 11:00:00 GMT+05:30 2012
In my database table the datetime column datatype is datetime. So how can I change the above output to datetime format to store it in database.
Any help in this regard will be thankful.
you should probably read a bit about SimpleDateFormat, it's the way to parse String into Date.
the way to do this is to create a pattern for the formatter, then create the formatter and after that parse the Strings.
String pattern = "EEE, dd MMM yyyy HH:mm:ss z";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date myDate = format.parse(str);

sqlite date format for android

I have a table in SQLITE with a DATETIME column.
I do a SQL statement which populates it with now()
I want to retreive it and parse it as a Date object in java, with following code:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat
("yyyy-MM-ddHH:mm:ss",Locale.ENGLISH);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
then I get the date via:
Date d = simpleDateFormat.parse
(recordset.getString(recordset.getColumnIndex("storedate")));
I get parse exception: unparceable date (and I guess it has to do with the format. Anyone can tell me which format it should be or where the error is?
Try this :
simpleDateFormat.format(
new Date(
recordset.getString(
recordset.getColumnIndex("storedate")
)));
I found out what it was. I had to change the format to this one:
"EEE MMM dd HH:mm:ss Z yyyy"
now it works. Guess SQLite is storing datetime objects in this format by default.

Date and time in android

In my appliaction I have to store current date into the database. How can i get the current date and is there is any specific format to store date in database.
Better would be to store the date/time in long in Database and then fetch the long date/time from Database and specify the required format using SimpleDateFormat.
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in
Greenwich on November 24, 4714 B.C. according to the proleptic
Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01
00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
Now for how to insert date in after it.
Use PreparedStatement#setString() or #setLong() respectively.
Hope this explanation works for you..
first convert the date to be stored to a String object using SimpleDateFormat class
code sample:
Date dateToBeStored = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); // this format will help you to convert date to 2012/07/04 format string
String dateString = formatter.format(dateToBeStored); // convert string
now read the date string from DB. you should have some means to get the date back from DB.
String readDateStringFromDB = readDate();
Now parse the read date string to date object by parse method of SimpleDateFormat class
Date dateObj = formatter.parse(readDateStringFromDB); // now you have the Date object back
SimpleDateFormat sdfDateTime = new SimpleDateFormat("dd/MM/yyyy"+ "",Locale.US);
String newtime = sdfDateTime.format(new Date(System.currentTimeMillis()));
You can use the Date class to get the epoch time, which could then be stored in the database as an integer. Alternatively you could convert the epoch time to regular time and store it as a date data type.
https://developer.android.com/reference/java/text/SimpleDateFormat

Categories

Resources