Java 如何在android中比较两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28555031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to compare two times in android
提问by
Two Dates are Comparing but Time is not comparing properly. This is my Code
两个日期正在比较,但时间未正确比较。这是我的代码
final String setdate = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4)));
final String settime = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(5)));
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy");
String currentDate = formatter1.format(calendar1.getTime());
Calendar calendar2 = Calendar.getInstance();
SimpleDateFormat formatter2 = new SimpleDateFormat("h:mm");
String currentTime = formatter2.format(calendar2.getTime());
if(currentDate.compareTo(setdate)>=0)
{
if(currentTime.compareTo(settime)>=0) {
myCheckBox.setChecked(true);
myCheckBox.setEnabled(false);
}
}
How can I compare two times.In database date and time field are different.So help me please.
我怎样才能比较两次。数据库中的日期和时间字段是不同的。所以请帮帮我。
采纳答案by Finava Vipul
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy h:mm");
String currentDate = formatter1.format(calendar1.getTime());
final String dateString = cursor.getString(4);
final String timeString = cursor.getString(5);
String datadb =dateString+" "+timeString;
// Toast.makeText(context,"databse date:-"+datadb+"Current Date :-"+currentDate,Toast.LENGTH_LONG).show();
if(currentDate.compareTo(datadb)>=0) {
myCheckBox.setChecked(true);
myCheckBox.setEnabled(false);
}
回答by iOSNoob
Very Simple. Convert both the value time1 and time2 in millisecond or minute and measaure difference.
很简单。以毫秒或分钟为单位转换值 time1 和 time2,并测量差值。
OR
或者
This Link for functionmight help you. Good Luck.
此功能链接可能对您有所帮助。祝你好运。
回答by Vatsal Shah
In Java there is a method which you can compare two date and time
在 Java 中有一种方法可以比较两个日期和时间
For you have to format your date and time in DATE format in JAVA.
因为您必须在 JAVA 中以 DATE 格式格式化您的日期和时间。
After that use below code
之后使用下面的代码
date1.compareTo(date2);
Hope above code will help you.
希望上面的代码能帮到你。
Let me know if you need more help from my side.
如果您需要我方面的更多帮助,请告诉我。
回答by Steve K
I think you're asking how to compose a datetime from two separate fields of date and time. It's pretty simple. You can use your SimpleDateFormat
to do it:
我想您是在问如何从日期和时间的两个单独字段中组合日期时间。这很简单。你可以用你SimpleDateFormat
的来做:
final String dateString = cursor.getString(4);
final String timeString = cursor.getString(5);
SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy h:mm");
Date dbDate = sdf.parse(dateString + " " + timeString);
int compareToNow = new Date().compareTo(dbDate);
回答by arshad shaikh
Try this:
尝试这个:
private boolean checktimings(String time, String endtime) {
String pattern = "HH:mm";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
Date date1 = sdf.parse(time);
Date date2 = sdf.parse(endtime);
if(date1.before(date2)) {
return true;
} else {
return false;
}
} catch (ParseException e){
e.printStackTrace();
}
return false;
}
回答by Ketan Ramani
public static boolean isTimeGreater(String date, String time) {
Date currentTime, pickedTime;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
String currentTimeString = sdf.format(new Date());
try {
currentTime = sdf.parse(currentTimeString);
pickedTime = sdf.parse(date + " " + time);
Log.e("Ketan", "Current Date: " + currentTime);
Log.e("Ketan", "Picked Date: " + pickedTime);
/*compare > 0, if date1 is greater than date2
compare = 0, if date1 is equal to date2
compare < 0, if date1 is smaller than date2*/
if (pickedTime.compareTo(currentTime) > 0) {
Log.e("Ketan", "Picked Date is Greater than Current Date");
return true;
} else {
Log.e("Ketan", "Picked Date is Less than Current Date");
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return true;
}