如何比较从采摘者到当前日期的日期和时间 [英] how to compare date and time got from pickers with current date

查看:77
本文介绍了如何比较从采摘者到当前日期的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

date = formatter.parse(dateInString);
				time = (Date) formatterTime.parse(timeInString);
				Calendar current = Calendar.getInstance();

				Calendar cal = Calendar.getInstance();
				/*
				 * cal.set(DatePicker.g, pickerDate.getMonth(), pickerDate.getDayOfMonth(), pickerTime.getCurrentHour(),
				 * pickerTime.getCurrentMinute(), 00);
				 */
				cal.set(date.getYear(),date.getMonth(),date.getDay(),time.getHours(),time.getMinutes());//error i think so 

				if (cal.compareTo(current) <= 0)
				{
					// setAlarm(cal);
					// The set Date/Time already passed
					Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
				}
				else
				{
					 setAlarm(cal);
					 db.addContact(new Contact_details(name1, mobile_number1, home_number1, company_name1, jobtitle1, emailid1, date, time));
				}



如果我设置的日期和时间更长,我也会得到无效的日期和时间?。有人说如何比较日期和时间


if i set date and time greater also i am getting invalid date and time ?.can anyone say how to compare date and time

推荐答案

您的问题是您的两个方法调用没有返回您认为的值,如文档 [ ^ ]。

getYear 返回自1900年以来的年度,即2014年的114年。

getDay 返回星期几,而不是月中的某天。

您的代码应为:

Your problem is that two of your method calls do not return the values that you think, as described in the documentation[^].
getYear returns the year since 1900, i.e. 114 for 2014.
getDay returns the day of the week, not the day of the month.
Your code should read:
Calendar cal = Calendar.getInstance();
cal.set(date.getYear()+1900,date.getMonth(),date.getDate(),time.getHours(),time.getMinutes());



虽然注意到所有这些方法现在都已被弃用。



所以当你说比较没有发生时,那不是真的;它确实发生了,但总是产生错误的结果。


Although note that all these methods are now deprecated.

So when you said that comparison didn't take place, that was not quite true; it did take place, but always produced the wrong result.


这篇关于如何比较从采摘者到当前日期的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆