问题描述
我尝试以hh:mm:ss a"格式比较两次.但它并没有按照我的想法真正起作用.我用谷歌搜索但找不到正确的答案.抱歉,因为我是编程新手."
"I try to compare two time in "hh:mm:ss a" format. But it not really work in what I think. I googled but couldn't find a proper answer. Sorry cause I'm new to programming."
我尝试像这样比较时间:
I try to compare the time like this:
String strSQL = "SELECT * FROM " + TABLE_SCHEDULE + " WHERE lecturer_id=? AND schedule_day=? AND schedule_endtime > ?";
schedule_endtime > ?
然而,比较忽略了 AM/PM 导致结果变成这样:例如.12:00:00 PM 大于 02:00:00 PM.
However, the comparison has ignored the AM/PM which caused the result become like this:eg. 12:00:00 PM is bigger than 02:00:00 PM.
希望大家能给点建议或者提供一些解决方案.欣赏一下.
Hope that you all can give some tips or provide some solution. Appreciate it.
推荐答案
不是比较格式化的字符串,而是比较以毫秒为单位的值.看看这个 将字符串转换回日期:
Instead of comparing the formatted string, compare the value in milliseconds. Take a look at this to convert the string back to date:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
然后一旦你有两个日期,你就可以像这样比较它们:
Then once you have two dates you can compare them like so:
boolean before = someDate.before(anotherDate);
或
boolean after = someDate.after(anotherDate);
甚至
someDate.getTime() < anotherDate.getTime();
旁注:当我存储日期时,我喜欢只存储毫秒值和时区.这样你就不用担心这样的事情了.
Side note: when I store dates, I like to just store the millisecond value and the time zone. That way you don't need to worry about things like this.
这篇关于Android - 时间比较“hh:mm:ss a"格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!