I have dates in a SQLite table that are stored in non-standard date formats. I need to be able to query by them. For example, records for today's date are 11/1/2015 in the "date" column and 2015-11-1 in the "sortDate" column.
My query needs to return the count of records from the past week. The following returns nothing: SELECT count(*) FROM Grades WHERE sortDate BETWEEN '2015-10-24' AND '2015-11-02'
I also get nothing from SELECT count(*) FROM Grades WHERE sortDate BETWEEN datetime('now', '-7 days') AND datetime('now')
I think the issue is that my dates are not padded to always have 2 month or date digits, as in YYYY-MM-DD. How would I query this existing data with these non-standard formats?
As Sqlite doesn't have a date type you will need to do string comparison to achieve this. For that to work you need to reverse the order - eg from dd/MM/yyyy to yyyyMMdd, using something like
where substr(column,7)||substr(column,4,2)||substr(column,1,2)
between '20101101' and '20101130'
I ended up getting all date strings from the database and dealing with them in Java. I just needed the count of how many entries there were within the past week, past two weeks, and past month. I wrote the following function to return those counts based on a provided ArrayList of strings.
Calendar today = Calendar.getInstance();
Calendar tomorrow = (Calendar) today.clone();
tomorrow.add(Calendar.DATE, 1);
Calendar backDateWeek = (Calendar) today.clone();
backDateWeek.add(Calendar.DATE, -7);
Calendar backDateTwoWeeks = (Calendar) today.clone();
backDateTwoWeeks.add(Calendar.DATE, -14);
Calendar backDateMonth = (Calendar) today.clone();
backDateMonth.add(Calendar.DATE, -30);
ArrayList<Calendar> calendarList = new ArrayList<Calendar>();
Calendar tmpCal;
String strSplit[];
int month;
int day;
int year;
int countWeek = 0;
int countTwoWeeks = 0;
int countMonth = 0;
for (String dateStr : dateStrings) {
strSplit = dateStr.split("/");
month = Integer.parseInt(strSplit[0]) - 1;
day = Integer.parseInt(strSplit[1]);
year = Integer.parseInt(strSplit[2]);
tmpCal = Calendar.getInstance();
tmpCal.set(Calendar.YEAR, year);
tmpCal.set(Calendar.MONTH, month);
tmpCal.set(Calendar.DAY_OF_MONTH, day);
if (tmpCal.after(backDateWeek) && tmpCal.before(tomorrow)) {
countWeek++;
countTwoWeeks++;
countMonth++;
} else if (tmpCal.after(backDateTwoWeeks) && tmpCal.before(tomorrow)) {
countTwoWeeks++;
countMonth++;
} else if (tmpCal.after(backDateMonth) && tmpCal.before(tomorrow)) {
countMonth++;
}
}
int[] countsArray = new int[3];
countsArray[0] = countWeek;
countsArray[1] = countTwoWeeks;
countsArray[2] = countMonth;
return countsArray;
Related
I was doing an Android app that generates sales report for dates between the current date and seven days in the past. It worked fine, here's the code:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR,-7);
String currentDate = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String sevenDayAgo = sdf.format(calendar.getTime());
Cursor weeklyIncome = db.getResult("select sum(price) as total_income from sales where date between '"+sevenDayAgo+"' and '"+currentDate+"'");
Cursor weeklyCost = db.getResult("select sum(purchase_price * quantity) as total_cost from sales where date between '"+sevenDayAgo+"' and '"+currentDate+"'");
Say for example currentDate = 31-08-2018 and sevenDayAgo = 24-08-2018 this all worked fine but when I change my system date to the next day which is the next month and currentDate becomes 01-09-2018 the query doesn't return anything from the database, it should have returned records between 25-08-2018 and 01-09-2018 which has seven days in between. Somehow the query doesn't work when the 7 days are in two different months. I don't know what's going on and how to fix it.
p.s. The date column in sales table is of type TEXT.
The problem is the format you're using for dates (dd-mm-yyyy) isn't in lexicographic order. The string '25-08-2018' compares greater than '01-09-2018' . x BETWEEN y AND z is equivalent to x >= y AND x <= z. That condition won't be true for dates in that range using your format (Remember, they're just strings. sqlite does not have a date type.
You should be using ISO-8601 formats, like yyyy-mm-dd. These will sort properly ('2018-08-25' < '2018-09-01') and will allow you to use the sqlite3 date and time functions on them.
Suppose user has entered the birth date in 31/08/2018 this format :
Parse it like :
String[] parts = BirthField.getText().toString().trim().split("/");
int CAL_DAY, CAL_MONTH, CAL_YEAR;
if(parts.length == 3)
{
CAL_DAY = Integer.parseInt(parts[0]);
CAL_MONTH = Integer.parseInt(parts[1]);
CAL_YEAR = Integer.parseInt(parts[2]);
}
You can check if user has entered a good date or not with :
if( CAL_DAY > 0 && CAL_DAY < 32 && CAL_MONTH > 0 && CAL_MONTH < 13 && CAL_YEAR > 1900 )
While storing in database table take below care :
Calendar CalendarEvent = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
CalendarEvent.set(Calendar.DATE, CAL_DAY);
CalendarEvent.set(Calendar.MONTH, CAL_MONTH - 1);
CalendarEvent.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ScheduleTime) );
CalendarEvent.set(Calendar.MINUTE, 00);
CalendarEvent.set(Calendar.SECOND, 00);
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.getDefault());
String DayConsidered = format.format(CalendarEvent.getTime());
Please note CAL_MONTH - 1 as system starts months from 0 to 11
While storing store in database in SystemTime like :
SYSTIME = CalendarEvent.getTimeInMillis();
While using it again retrieve it from database and initialise calendar event with it like :
Calendar CalendarEvent = Calendar.getInstance();
CalendarEvent.setTimeInMillis( Long.parseLong(SYSTIME) );
int CAL_DAY = CalendarEvent.get(Calendar.DAY);
CalendarEvent.set(Calendar.DATE, CAL_DAY - 7);
And it will start working for even if days / months / even you change years from any date; it will always show the date which is less than 7 days
Hope it helps and let me know how it works as i am answering it quick...
Edit : 2
Better and most efficient way which i am using in my project and working perfectly is :
Log.d("ADDING A MONTH :", "ADDING 1 MONTH....\n");
CalendarEvent.add(Calendar.MONTH, 1);
Log.d("NEGATING 7 DAYS :", "NEGATING 7 DAYS....\n");
CalendarEvent.add(Calendar.DATE, -7);
I am new to Android.I have a requirement, I have a field to enter the Date Of Birth of a person.On successful selection I wanna return the total number of months from the DOB to current date.For example, if I entered DOB as 19/10/2012 I wanna return 36(months).I searched for this, but didn't find anything suitable to my requirement.Here is my current code which return sucessful data,
private void showDate(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day);
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
if(System.currentTimeMillis() > date.getTime()) {
edtDate.setText(sdf.format(date));
LocalDate date1 = new LocalDate(date);
LocalDate date2 = new LocalDate(new java.util.Date());
PeriodType monthDay = PeriodType.yearMonthDayTime();
Period difference = new Period(date1, date2, monthDay);
int months = difference.getMonths();
months=months + 1;
System.out.println("16102015:Nunber of Months"+months);
}else{
Toast.makeText(mActivity,getResources().getString(R.string.date_validationmsg),Toast.LENGTH_LONG).show();
}
}
Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
To start with, I'd suggest using LocalDate instead of DateTime for the computations. Ideally, don't use java.util.Date at all, and take your input as LocalDate to start with (e.g. by parsing text straight to that, or wherever your data comes from.) Set the day of month to 1 in both dates, and then take the difference in months:
private static int monthsBetweenDates(LocalDate start, LocalDate end) {
start = start.withDayOfMonth(1);
end = end.withDayOfMonth(1);
return Months.monthsBetween(start, end).getMonths();
}
UPDATE 1
see this link the OP is accepted the same answer because Months.monthsBetween() method is not working proper for him
UPDATE 2
LocalDate userEnteredDate = LocalDate.parse( new SimpleDateFormat("yyyy-MM-dd").format(date));
LocaleDate currentDate = LocalDate.parse( new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
int months = monthsBetweenDates(userEnteredDate, currentDate)
Using Joda-time library here, I was able to get the desired result.
Try the below code it would give the desired the difference in months.
DateTime date1 = new DateTime().withDate(2012, 10, 19);
DateTime today = new DateTime().withDate(2015, 10, 19);
// calculate month difference
int diffMonths = Months.monthsBetween(date1.withDayOfMonth(1), today.withDayOfMonth(1)).getMonths();
Using JodaTime, it's really easy:
http://www.joda.org/joda-time/apidocs/
int nMonths = new Period(startTime, endTime).getMonths();
Use this code to calculate months between two dates
public static int monthsBetweenUsingJoda(Date d1, Date d2) {
return Months.monthsBetween(new LocalDate(d1.getTime()), new LocalDate(d2.getTime())).getMonths();
}
In my application i am displaying a calendar of days in a horizontal scrollable listview like below :
The dates are proper and the current date is also selected, the issue i am facing is the week day that is displayed.It is not proper. The code written to displayed this kind of calendar is as follows:
int count = 0;
for (int i = 1; i <= noOfDays; i++) {
int year = Calendar.YEAR;
int month = Calendar.MONTH;
int day = i;
Calendar c = new GregorianCalendar(year, month, day);
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String d = sdf.format(cal1.getTime());
CustomData custom = new CustomData(String.valueOf(i),
getWeekday(c.get(Calendar.DAY_OF_WEEK)), d);
mCustomData[count] = custom;
Log.e("mCustomData", d);
count++;
if(Integer.parseInt(splitDateee[0])==i)
{
currentDate = i-1;
}
}
There is an error in weekdays that is being displayed. What am i missing here? Not able to figure out the issue.
Please help ! Thanks in Advance!
int year = Calendar.YEAR;
int month = Calendar.MONTH;
These are flags belonging to calendar to get and set values, and do not indicate the current year and month. You would need to get the year and month from the current device time and do:
int year = currentYear;
int month = currentMonth;
Calling Calendar.getInstance() gets the calendar for the current day:
Calendar now = Calendar.getInstance();
You can then use the flags like so:
int month = now.get(Calendar.MONTH);
int year = now.get(Calendar.YEAR);
I want to save system date on my database, but it returns : 22/0/2013.
here my code :
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month_ = cal.get(Calendar.MONTH);
int day_ = cal.get(Calendar.DATE);
String FullDate = (""+day_+"/"+month_+"/"+year);
String text_Rate=(String.valueOf(FullDate));
Log.d("System Date show", text_Rate);
where i'm doing wrong.
This is the correct behavior. The Java Calendar month is 0-based (January is 0, December is 11).
If you really want to store it as 22/1/2013, simply add +1 to your month:
int month_ = cal.get(Calendar.MONTH) + 1;
you should save dates as long to sqlite. that is easier and less prone to errors. see here for calendar to long.
In my app I have a date saved in a remote database that I want the date picker to be set to. I've researched and only found examples of setting the datepicker today's date via Calender java util. Example:
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
How can I use the Calendar to display my date from the database and not today's date? Do you have any suggestions or examples I can follow?
Update:
After experimenting with Calendar I tried to use
// set Date
String eventYear =date.substring(0,4);
String eventDay =date.substring(5,7);
String eventMonth =date.substring(8,10);
//convert string to int for because calendar only takes int: set(int,int)
int month = Integer.parseInt(eventMonth);
final Calendar c = Calendar.getInstance();
mMonth=c.get(c.set(Calendar.MONTH, Calendar.month));
// or mMonth=c.get(Calendar.MONTH, Calendar.month);
Generates error that says cannot convert int to void.
How can I use Calendar to set it to a specific date? According to google's developers site I should be able to do this.
http://developer.android.com/reference/java/util/Calendar.html
example:
set(Calendar.MONTH, Calendar.SEPTEMBER)
I'd like the date to be display in the datepicker from the server as a default value.
U can use the updateDate(year, month, day_of_month);
date picker returns integer values of day, month and year. so the parameters must be integer values. and the integer value for the month jan in the date picker is 0.
i needed to put the date extracted from a database into a datepicker. I wrote the following code and it works.
DatePicker DOB;
SQLiteDatabase db;
DOB=(DatePicker)findViewById(R.id.datePicker1);
db = openOrCreateDatabase("BdaySMS", SQLiteDatabase.CREATE_IF_NECESSARY, null);
Cursor cur = db.rawQuery("select * from BdaySMS where ph='"+pn+"';", null);//pn is the phone no.
if(cur.moveToFirst())
{
name.setText(cur.getString(0));
phone.setText(cur.getString(1));
DOB.updateDate(Integer.parseInt(cur.getString(4)),Integer.parseInt(cur.getString(3)),Integer.parseInt(cur.getString(2)));
message.setText(cur.getString(5));
}
Use JodaTime
Here's a simple example of how I set a DatePicker and TimePicker from a DateTime object, which could be the current date or any date from the past or future (the attribute in this case is called inspected_at):
DatePicker dp = (DatePicker) findViewById(R.id.inspected_at_date);
TimePicker tp = (TimePicker) findViewById(R.id.inspected_at_time);
DateTime inspected_at = DateTime.now().minusYears(1); // Typically pulled from DB.
int year = inspected_at.getYear() ;
int month = inspected_at.getMonthOfYear() - 1; // Need to subtract 1 here.
int day = inspected_at.getDayOfMonth();
int hour = inspected_at.getHourOfDay();
int minutes = inspected_at.getMinuteOfHour();
dp.updateDate(year, month, day);
tp.setCurrentHour(hour);
tp.setCurrentMinute(minutes);
Hope that helps.
JP