I need to use the before
method in Java. So I can do date compare code like this:
if (storedDate.before(currentMonth)) {
}
Where currentMonth
is set like this:
int thisMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
cal = Calendar.getInstance(); df = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
cal.set(Calendar.MONTH, thisMonth);
// Do I even need to convert like this to match what is stored in the DB below?
And storedDate
loops through a SQLite
table where dates are stored as formatted Strings such as "Nov 2013" or "Dec 2014"; yes, bad design I know.
What I need to do is see if the date in the current row in the loop is older than this month; if so, I will delete it out of the SQLite DB (I have that code which is fine).
So, how can I build the two variables where I can compare like this if (storedDate.before(currentMonth)) {
?
EDIT:
This is how the month is stored into the DB:
monthTotal = monthTotal + 1;
Calendar myDate = Calendar.getInstance();
myDate.add(Calendar.MONTH, monthTotal);
SimpleDateFormat dfEng = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
String finalDBDate = dfEng.format(myDate.getTime());
EDIT2:
Here is what I have so far
private void deleteOldSpecialPayments() {
int thisMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
cal = Calendar.getInstance();
df = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
cal.set(Calendar.MONTH, thisMonth);
String thisMonthS = df.format(cal.getTime());
Date currentDate = null;
try {
currentDate = df.parse(thisMonthS);
} catch (ParseException e) {
e.printStackTrace();
}
if (!database.isOpen()) {
open();
}
Cursor cursor = database.query(MySQLiteHelper.TABLE_CUSTOM_PAYMENTS, allLedgerColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
SpecialPayment sp = cursorToCustomPayments(cursor);
try {
Date d = df.parse(sp.month);
if (d.before(currentDate)) {
// DELETE ROW
}
} catch (ParseException e) {
e.printStackTrace();
}
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
}
It's not entirely clear what value you want to compare. It's quite easy to see whether "now" is later than the start of the 1st of a particular month:
// TODO: For testing purposes, you'd want a Clock abstraction to be injected.
Date now = new Date();
DateFormat format = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
// Each row...
for (...) {
Date storedDate = format.parse(textFromDatabase);
if (now.compareTo(storedDate) >= 0) {
// It is now on or after the start of the given month
}
}
However, you may not want the start of the month stored in the database - you might want the start of the next month. For example, if the stored month is "July 2015" then you might want to delete the row as soon as it's the start of July in the user's time zone - or you may want to wait until it's the start of August. For the start of August, you could either use java.util.Calendar
(or ideally Joda Time) to add a month to the stored date, or you could parse the month and year separately as Elliott suggested in comments.
See more on this question at Stackoverflow