Java 使用before方法比较两个日期(格式化为字符串的日期和当前时间)
我需要在Java中使用Java 使用before方法比较两个日期(格式化为字符串的日期和当前时间),java,android,date,compare,Java,Android,Date,Compare,我需要在Java中使用before方法。因此,我可以执行如下日期比较代码: if (storedDate.before(currentMonth)) { } int thisMonth = Calendar.getInstance().get(Calendar.MONTH) + 1; cal = Calendar.getInstance(); df = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH); cal.set(Calendar.MON
before
方法。因此,我可以执行如下日期比较代码:
if (storedDate.before(currentMonth)) {
}
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?
其中currentMonth
设置如下:
if (storedDate.before(currentMonth)) {
}
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?
和storedDate
循环通过SQLite
表,其中日期存储为格式化字符串,如“2013年11月”或“2014年12月”;是的,我知道设计很糟糕
我需要做的是查看循环中当前行中的日期是否早于本月;如果是这样,我将从SQLite数据库中删除它(我有这段代码,这很好)
那么,我如何构建这两个变量,在这里我可以像这样比较if(storedDate.before(currentmount)){
编辑:
这是如何将月份存储到数据库中的:
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:
这是我到目前为止所拥有的
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();
}
您想要比较的值并不完全清楚。很容易看出“现在”是否晚于某个月的第一个月开始:
// 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
}
}
但是,您可能不希望将月初存储在数据库中-您可能希望下个月的月初。例如,如果存储的月份为“2015年7月”然后,您可能希望在用户时区的7月初删除该行,或者您可能希望等到8月初。对于8月初,您可以使用
java.util.Calendar
(或者理想情况下)要在存储的日期中添加月份,或者您可以按照Elliott在评论中的建议分别解析月份和年份。为什么要在数据库中以字符串形式存储这些值?如果有,为什么不使用它?这比解析日期要简单得多。(这也消除了模糊性——你有一个月和一年……你对这个月和一年中的哪一个瞬间感兴趣,以及你感兴趣的时区?)是的,结果证明这是无用的。那么,回到上一期:你对这个月的哪个部分感兴趣,在哪个时区?@JonSkeet对UNIX时间混乱表示抱歉,我删除了这些引用。我更像是一个PHP程序员,而不是Java。在PHP中有一个strotime和strfortime函数,基本上可以转换和处理str很好地定义日期。我正在寻找这样的东西…@kicking莴苣解析年份。比较年份。如果一年少于一年,那就是你的结果。否则解析月份,然后比较月份。如果一个月少于一个月,那就是你的结果。否则,它们是一样的。你没有日期或时间(只有一年和一个月)。