Java 如何检查:dateOne<;日期二
我需要帮助检查以下与日期和时间相关的条件Java 如何检查:dateOne<;日期二,java,validation,datetime,calendar,Java,Validation,Datetime,Calendar,我需要帮助检查以下与日期和时间相关的条件 Calendar cal = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); String CurrentDate= dateFormat.format(cal.getTime()); String ModifiedDate=从日期n时间选择器小部件获取的日期时间 我必须检查一下: 当前修改日期不少于
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String CurrentDate= dateFormat.format(cal.getTime());
String ModifiedDate=从日期n时间选择器小部件获取的日期时间强>
我必须检查一下:
当前修改日期不少于当前时间的5分钟
如何在Android/Java中检查此条件……?要检查给定时间是否在当前时间之前/之后, Android中有一个日历实例…用于比较日期时间值
Calendar current_time = Calendar.getInstance ();
current_time.add(Calendar.DAY_OF_YEAR, 0);
current_time.set(Calendar.HOUR_OF_DAY, hrs);
current_time.set(Calendar.MINUTE, mins );
current_time.set(Calendar.SECOND, 0);
Calendar given_time = Calendar.getInstance ();
given_time.add(Calendar.DAY_OF_YEAR, 0);
given_time.set(Calendar.HOUR_OF_DAY, hrs);
given_time.set(Calendar.MINUTE, mins );
given_time.set(Calendar.SECOND, 0);
current_time.getTime();
given_time.getTime();
boolean v = current_calendar.after(given_calendar);
// it will return true if current time is after given time
if(v){
return true;
}
你为什么要格式化日期
使用“自然”表示法比使用字符串表示法更容易。不清楚是否必须将修改后的日期作为字符串,但如果是这样,则首先应该对其进行解析。然后,您可以使用以下方法将其与当前日期和时间进行比较:
// Check if the value is later than "now"
if (date.getTime() > System.currentTimeMillis())
或
现在还不清楚“当前ModifiedDate不少于当前时间的5分钟”是什么意思——不管你是指不少于5分钟之后,还是不少于5分钟之前,或者诸如此类——但是你应该能够更改上面的代码来处理你的需求
如果您进行了大量的日期/时间操作,我强烈建议您使用,这是一种比java.util.date
/Calendar
更好的日期/时间API
public static boolean getTimeDiff(Date dateOne, Date dateTwo) {
long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
int day = (int) TimeUnit.MILLISECONDS.toHours(timeDiff);
int min= (int) ( TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
if(day>1)
{
return false;
}
else if(min>5)
{
return false;
}
else
{
return true;
}
}
用法:
System.out.println(getTimeDiff(new Date("01/13/2012 12:05:00"),new Date("01/12/2012 13:00:00")));
这可以帮助您(检查
的执行情况检查日期是未来的
)这不是关于android的
System.out.println(getTimeDiff(new Date("01/13/2012 12:05:00"),new Date("01/12/2012 13:00:00")));