Problem comparing dates (times) in Android(在 Android 中比较日期(时间)时出现问题)
问题描述
我在尝试比较 Android 中的两个日期时遇到问题.我不确定这是模拟器的问题还是我的代码本身有问题.问题是代码在普通的 Java 程序环境中工作,这让我更加困惑.
I am having a problem when i am trying to compare two dates in Android. I am not sure if it is a problem with the emulator or if i have a problem in the code itself. The thing is the code works in a normal Java program environment, which confuses me even more.
我有以下代码来比较 Android 2.1 中的日期:
I have the following code to compare dates in Android 2.1 :
public boolean compareDates(String givenDateString) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
boolean True;
try {
True = false;
Date givenDate = sdf.parse(givenDateString);
Date currentDate = new Date();
if(givenDate.after(currentDate)){
True = true;
} if(givenDate.before(currentDate)){
True = false;
} if(givenDate.equals(currentDate)){
True = false;
}
} catch (Exception e) {
Log.e("ERROR! - comparing DATES", e.toString());
}
return True;
}
现在代码在 Java 中运行良好,但在 Android 中它一直返回错误.当我用这样的字符串插入 currentDate 变量时,唯一的变化发生了:
now the code works well in Java, but in Android it keeps returning me false. The only change happens when i insert the currentDate variable with a string like this:
Date currentDate = sdf.parse("16:50");
在字符串中设置 currentDate 变量时,当我将它与给定时间之后的值进行比较时,它会返回 true.我也尝试过设置 currentDate 变量:
With the currentDate variable set in a string it returns true when i compare it with a value that's after the time given. I have also tried setting the currentDate variable with:
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
我在这里完全不知所措.希望有人对这里可能存在的问题有任何想法.
I am completely at a loss here. Hoping someone has any ideas on what might be a problem here.
--- 编辑---
找到了我的问题的解决方案.我使用日历并从那里读取小时和分钟,然后将它们放在一个字符串中进行解析并且它起作用了.代码现在看起来像这样:
Found the solution for my problem. I used the calendar and read the hour and minute from there, then i put them in a string to parse and it worked. The code now looks like this:
public boolean compareDates(String givenDateString) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
boolean True = false;
try {
Date givenDate = sdf.parse(givenDateString);
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
Date currentDate = sdf.parse(hour + ":" + minute);
if(givenDate.after(currentDate)){
True = true;
} if(givenDate.before(currentDate)){
True = false;
} if(givenDate.equals(currentDate)){
True = false;
}
} catch (Exception e) {
Log.e("ERROR! - comparing DATES", e.toString());
}
return True;
}
推荐答案
当我对日期进行比较时,我确保使用 Calendar 对象.然后我使用 compareTo() 函数:
When I'm doing comparisons on dates, I make sure to use Calendar objects. Then I use the compareTo() function:
if ( myCalendar.compareTo(upperLimitCalendar) >= 0 )
在此处查看文档:
http://developer.android.com/reference/java/util/日历.html
这篇关于在 Android 中比较日期(时间)时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Android 中比较日期(时间)时出现问题
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01