I have the following fields
1> WorkName - Varchar
2> TimeStap
I wanted to create a Table with the above fields.
What will be the TimeStamp datatype
How can I insert timestamp values to the table.
What will the timestamp value while inserting data or how to fetch the timestamp.
I have worked on SQLite but don't have any experience on adding TimeStamp as a field to the table & adding values to that.
What kind of CREATE & INSERT statements should I use?
There are limited datatypes available in a Sqlite Database, so the one I find useful for dates is integer - this will accept long values (dynamically adjusts its size) so for dates store the milliseconds value of a date and the date can be easily reconsituted when reading the millisecond values back out of the database.
I create timestamp by this:
/**
*
* #return yyyy-MM-dd HH:mm:ss formate date as string
*/
public static String getCurrentTimeStamp(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTimeStamp = dateFormat.format(new Date()); // Find todays date
return currentTimeStamp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
You could use Java.sql.Timestamp class as the column datatype of your table in a SQL database.
Since SQLiteDatabase class doesn't support a put() method for Timestamp variables, use the getTime() method in the class to insert the corresponding milliseconds of that time as a long value.(SQLiteDatabase does have a put() method for long type)
Open this for the method reference.
Related
i am trying to change that datetime format that i get from db in sqlite3
String[] st1=new String[] {"_id", "DREPORT"};
int str2[]={ R.id.mainTitle, R.id.textView2};
SimpleCursorAdapter adapterc=new SimpleCursorAdapter(this,R.layout.customlistview,c, st1,str2);
lv1=(ListView)findViewById(R.id.lv1);
lv1.setAdapter(adapterc);
the _id (is date) and show as
image of listview date format
id like to display it only as mm/dd or yyyy/mm/dd
the query:
public Cursor GetReports(String month)
{
String[] n1={"dt AS _id","DREPORT"};
Cursor c1=db.query(DbHelper.reportt,n1,null,null,null,null,null,null);
return c1;
}
is there a way to change the query to return other date format ?
or should i just change the filed to String and save it after format change as string
?
the _id (is date) and show as yyyy/mm/dd hh:mm:ss....
Well, SQLite doesn't like that format.
If possible, you need to reformat the database to hold YYYY-MM-DD HH:MM:SS
id like to display it only as mm/dd or yyyy/mm/dd
Once you store as mentioned, you can run strftime(date_format, timestring).
For example,
sqlite> SELECT strftime('%m/%d', datetime('now'));
02/26
sqlite> SELECT strftime('%Y/%m/%d', datetime('now'));
2017/02/26
As far as Android goes...
String[] n1={"strftime(" + dateFormatToShow + ", dt) AS _id","DREPORT"};
You can convert your date in any format you want using this code
public static String formatDateTime(String date, String fromFormat, String toFormat) {
Date d = null;
try {
d = new SimpleDateFormat(fromFormat, Locale.US).parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return new SimpleDateFormat(toFormat, Locale.US).format(d);
}
You just need to pass "date", "fromFormat" (current of the date) and "toFormat" (the format you want).
In your case you can pass
date: yourDate which you want to format
fromFormat: "yyyy/mm/dd hh:mm:ss"
toFromat: "yyyy/MM/dd"
You can use this function to convert any format date to any format you need
"I try to compare two time in "hh:mm:ss a" format. But it not really work in what I think. I googled but couldn't find a proper answer. Sorry cause I'm new to programming."
I try to compare the time like this:
String strSQL = "SELECT * FROM " + TABLE_SCHEDULE + " WHERE lecturer_id=? AND schedule_day=? AND schedule_endtime > ?";
schedule_endtime > ?
However, the comparison has ignored the AM/PM which caused the result become like this:
eg. 12:00:00 PM is bigger than 02:00:00 PM.
Hope that you all can give some tips or provide some solution. Appreciate it.
Instead of comparing the formatted string, compare the value in milliseconds. Take a look at this to convert the string back to date:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Then once you have two dates you can compare them like so:
boolean before = someDate.before(anotherDate);
or
boolean after = someDate.after(anotherDate);
or even
someDate.getTime() < anotherDate.getTime();
Side note: when I store dates, I like to just store the millisecond value and the time zone. That way you don't need to worry about things like this.
Inside the SQLite database you are storing the dates as Strings, not as a Date because in SQLite doesn't exist a Date type (https://www.sqlite.org/datatype3.html).
You have two options: change the column type to a INTEGER type and store de date as a number (then you can compare milliseconds) or get the entity as it is, parse a Date type with the String, create a SimpleDateFormat with the given format and then make the comparison.
I want to retrieve the date from sqlite by using date. But by default the column in sqlite inserted is datetime format.
For instance,this is my Type Table
_id date_value
1 2015-07-06T11:36:35
2 2015-07-06T12:10:10
3 2015-07-06T12:10:19
4 2015-07-03T17:13:59
5 2015-07-03T17:14:04
6 2015-07-03T17:14:27
I want to retrieve the data by using date only. How do i want to query?
I tried to retrieve like this: Select * from Type where date_value=date('2015-07-03T17:13:59')
It returns zero rows.
I need all rows where the date(2015-07-03) matched
Expected result:
_id date_value
4 2015-07-03T17:14:27
5 2015-07-03T17:14:04
6 2015-07-03T17:14:27
How should i achieve this output?
The date() function simply returns the date part of its parameter, so the result of date('2015-07-03T17:13:59') is '2015-07-03'. This value does not match any of the values in the date_value column.
You need to extract the date from the values in the date_value column:
Select * from Type where date(date_value) = '2015-07-03'
Use SimpleDateFormat to format the Date as a String:
DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
String todayAsString = dateFormat.format(today);
String tomorrowAsString = dateFormat.format(tomorrow);
System.out.println(todayAsString);
System.out.println(tomorrowAsString);
Prints:
2015-07-03 16:14:27
2015-08-03 16:14:27
I have a Database that when ever i put in information about things i want the database to show a time stamp with the information that i put in.
I'm assuming when you say "put in information" you're talking about an insert. If that's the case, you can just create a datetime column with the default of now in your schema:
DATETIME DEFAULT CURRENT_TIMESTAMP
You can also set it on your insert:
INSERT INTO your_database VALUES (datetime())
See here for more details:
How to insert a SQLite record with a datetime set to 'now' in Android application?
Good luck!
The simplest option might be to do it manually. Add an integer column and store System.currentTimeMillis() in that column.
EDIT
If you want to store it human readable create a text column. Then create the timestamp something like this
SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy HH:mm:ss.S z");
Date now = new Date();
String timestamp = sdf.format(now);
which creates a String like this (you can change the format, check out the SimpleDateFormat documentation)
9 Dec 2011 23:06:05.849 EST
and if you are reading from the database you can do this to get a date object
Date then = null;
try {
then = sdf.parse(timestamp);
} catch (ParseException e) {
e.printStackTrace();
}
If you don't really care about the format you can just do
String timestamp = new Date().toString();
which will give output like
Fri Dec 09 23:06:05 EST 2011
i am working on data manipulation using sqlite.
how can i insert the datetime value in sqlite
i implemented the below code in my app.
it works,
SimpleDateFormat curFormater = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
Date dateObj = null;
dateObj =curFormater.parse(txt_date_start.getText().toString());
SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
String newDateStr = postFormater.format(dateObj);
but I want to store as "2011-Jul-13 05:15 PM",ple give me example..
thanks in advance
in sqlite you dont have any data type as date or time.while creating the column u have to specify any one of the text,int or real data type.then while inserting make use of the date and time funcs of sqlite.
Create column in sqlite's table with type TIMESTAMP/DATE and put the date in UNIX timestamp.