Working with date in sqlite on android - android

When I run this query in sqlite tools I have a correct answer but when I run this query in my android project it returns 0.
Do you know what is problem?
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar strDate = Calendar.getInstance();
Calendar dateAgo = Calendar.getInstance();
dateAgo.add(Calendar.DATE, -7);
String query1 = "SELECT sum(Income.Money) AS 'Report' " +
" FROM Income " +
" WHERE Income.[Date] > '"+dateFormat.format(dateAgo.getTime())+
"' AND Income.[Date] <= '"+dateFormat.format(strDate.getTime())+"'";
I checked all of my code but they don't have any mistake.

MM/dd/yyyy is not a valid Time String (see: https://www.sqlite.org/lang_datefunc.html).
Use this format: yyyy-MM-dd.

Related

Format date like system format using Time.getCurrentTimezone()

I'm using Time.getCurrentTimezone()to get the current timezone and therefore the date.
I'm getting and formatting it like this:
private void setDate() {
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
tvdate2.setText(today.monthDay + "-" + today.month + "-" + today.year);
}
How can I format the date according to the system settings?
I didn't manage to get it done via SimpleDateFormat...
You could get time in any format by using SimpleDateFormat class. Please refer to this
Calendar cal = Calendar.getInstance();
String currentDate = cal.get(Calendar.DAY_OF_MONTH)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.YEAR);
Date date = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.ENGLISH).parse(currentDate);

How to insert current date in sqlite from android application

I want to insert current date in SQLite database datetime type field.
How can I do this?
UPDATE : Using mDb.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (datetime()) ");
I am able to insert like date_created=2012-10-10 13:02:08 and I want this format 10 Oct 2012 12:48 how to achive this?
I prefer the following ways to get it done:
Create the table with a default settings to put current datetime
automatically. For example:
ColumnName DATETIME DEFAULT CURRENT_TIMESTAMP
Create SimpleDateFormat and convert current date into SQL format
string. For example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(new Date());
ContentValues values = new ContentValues();
values.put("ColumnName", strDate);
You can use:
mDb.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (datetime()) ");
check: https://stackoverflow.com/a/819605/1434631
ContentValues coloumnVlues = new ContentValues();
//SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
Date date = new Date();
coloumnVlues.put("fname", fname);
// inserted date in database like May 31, 2013 10:42:53 AM it's simulator datetime
coloumnVlues.put("created_date", DateFormat.getDateTimeInstance(). format(date));
their is no need to use SimpleDateFormat.
you get datetime from database the example given in below link
http://www.sqlite.org/lang_datefunc.html

Android Date displaying

I've a problem when displaying the date. I do everything fine, the date shows...bad it's not correct!
It says 2012/07/30 when it should be 2012/08/30.
I've checked my date in the mobile and it's correct. Do you have any idea?
Thank you!!
Piece of code:
Calendar c = Calendar.getInstance();
String sDate = c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH) + "/" + c.get(Calendar.YEAR);
view2.setText("" + et.getText() + sDate );
why don't you use DateFormat
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
view2.setText(dateFormat.format(c.getTime());
The Calendar object does not work as you are expecting it. The get(Calendar.MONTH) returns a constant representing the month of the date, not the number of the month. This subtle difference can be seen here:
java.util.calendar
As you will see, the constants reflect a zero-based sequence, starting with January, so that you can use the constants as indexers into an array for month-based lookups.
For your purposes, you could get away with adding 1 to the returned int, but you might want to look at SimpleDateFormat in the java.text. This article provides a brief overview to get you started.
Good luck!
You can instead use this,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String currentDateandTime = sdf.format(new Date())

How to Compare system date with mydate in android 2.1?

in my Android application, i am taking date and time from database. but i am not able to get the date in the "Date" Format from the database into my application, the date is in string format, so i am not able to compare the system date to database date.
if i convert the system date into string then i am not able to update the date into the database in recurring case. i also want to update the database date if the system date and database date is matched.
how can i achieve this is android.
Thanks in advance.
You can convert String to Date like this:
String str = "12/12/1912";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);
And back to String
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date is : " + formatter.format(date));
And Date has before and after methods and can be compared to each other.
By the way there is also a library called Joda, you can also check it out.
Try this code:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM");
formattedDate = df.format(c.getTime());

Comparing the Date in Android

I am using SQLITE database and I store the date as a String. Now I want to compare both the string as a date. While I am using
String sqlQuery = "SELECT title,edate FROM lookup WHERE" + ((Date)df.parse("?")).getTime() + "<=" + date1.getTime();
c= db.rawQuery(sqlQuery,new String[]{"edate"});
it is giving error at run time. Please Tell me how can I compare two String as a Date.
Thank You
Deepak
You want numbers in order to compare easily. I recommend POSIX (or unix) time, which counts seconds since a fixed point in about 1970.
Use an SQLite strftime function to convert your string to POSIX epoch time (check out the %s option) http://sqlite.org/lang_datefunc.html
Then use the Java functions to get epoch time: http://download.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()
"SELECT title, edate FROM lookup WHERE strftime(edate,'%s') < " + (Date1.getTime()/1000)
String formatString = "dd-MM-yyyy"; // for example
SimpleDateFormat df = new SimpleDateFormat(formatString);
Date date1 = df.parse(string1);
Date date2 = df.parse(string2);
if (date1.before(date2)) {
System.out.println("first date is earlier");
}

Categories

Resources