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.
Related
I am working on an app where I am saving date in currentTimeMillis() in sqlite database and when I read it I try to convert it to readable format. But date is always being converted to Jan 01, 1970.
Following is my code of Saving it to sqlite database:
long date = System.currentTimeMillis();
I then try to convert it to readable format in following way:
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(attendance.getPicDate());
Date resultdate = new Date(attendance.getPicDate());
System.out.println(sdf.format(resultdate));
I am getting the following output:
Jan 17,1970 19:36
Edit1: attendance.getPicDate() value = 1432678159
Edit2: when I try to save value it is 1500378235812 but when I read it, it becomes above, My column in table is Integer.
1432678159 does convert to Jan 17,1970 19:36.
Try checking attendance.setPicDate().
I found my mistake I was using cursor.getInt(2) to fetch date from database but now I have corrected it to cursor.getLong(2) and it is working great now.
I suggest, you should save date and time in string format in database.
Calendar calendar=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String currentTime=sdf.format(calendar.getTime());
save that "currentTime" in your database.
In my Android App, I want to insert date from DatePicker to SQL Server DB.
The Date type in SQL Server is datetime.
I can get Day and Month and Year from DatePicker like this :
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
But how should i pass it to SQL Server 2012?
With which format?
It depends on which database you want to store. If you are storing locally in your sqlite database I will recommend store the date in millis and if you are storing in backend server you can convert it to ISO date format. Examples of both way is given below, and if you want date in any other format you can do that too by modifying string passed to constructor of STANDARD_UI_DATE_FORMAT object.
Calender startTimeCalender = Calendar.getInstance();
startTimeCalender.set(Calendar.YEAR, year);
startTimeCalender.set(Calendar.MONTH, monthOfYear);
startTimeCalender.set(Calendar.DAY_OF_MONTH, dayOfMonth);
//Date in Millis
lond dateInMillis = startTimeCalender.getTimeInMillis();
SimpleDateFormat STANDARD_UI_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
//Date in ISO Format
String formattedDate = STANDARD_UI_DATE_FORMAT.format(startTimeCalender.getTime())
You can convert a string (as in the example provided by himanshu1496) to a valid datetime type on SQL Server using the CONVERT() function.
So as long as you can get the string value to the database you could do something like this:
SELECT
'2015-11-24 14:17:19' AS UnconvertedText,
CAST('2015-11-24T14:17:19' AS datetime) AS UsingCast,
CONVERT(datetime, '2015-11-24T14:17:19', 126) AS UsingConvertFrom_ISO8601 ;
GO
(Dont forget that sneaky T in the string in the example above)
It is more detailed described in the documentation here:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Save the date as miles into your SQL database.
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 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.