比较两个日期与时间的甲酸盐是YYYY-MM-DD HH:MM:SS在Android中 [英] Compare two dates with time whose Formate is yyyy-MM-dd HH:mm:ss in Android

查看:288
本文介绍了比较两个日期与时间的甲酸盐是YYYY-MM-DD HH:MM:SS在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要比较两个日期,其格式为 YYYY-MM-DD HH:MM:SS 。我知道比较日期只有之前或之后日期功能的方式。

I have to compare two dates whose format is yyyy-MM-dd HH:mm:ss. I know the way to compare date only the before or after date function.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentDateandTime = sdf.format(new Date());
String expiryTime = "2014-09-10 00:00:00";

但是,什么是要比较的日期和时间与当前的日期和时间的最好方法。
就像我们有两个日期2014年9月10日00:00:00和当前日期时间为2014年8月31日10:37:15。现在,我们必须进行比较。我们怎样才能做到这一点。

But what's the best way to compare date and time with the current date and time. Like we have two dates 2014-09-10 00:00:00 and current date with time is 2014-08-31 10:37:15. And now we have to compare it. How we can do that.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

使用转换日期字符串 java.util.Date 对象的 SimpleDateFormat的和比较的 日期#后 或的 日期#之前 方法。

Convert the Date String to java.util.Date object using SimpleDateFormat and compare those date objects with Date#after or Date#before methods.

的Java 8 - 使用新的的Java API时间,采用解析日期字符串 DateTimeFormat 并获得 LOCALDATE 对象和比较它们。

In java 8 - using new Java Time API, parse date String using DateTimeFormat and get LocalDate object and compare them.

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
final LocalDate dt1 = dtf.parseLocalDate(dateString1);
final LocalDate dt2 = dtf.parseLocalDate(dateString2);

final boolean check = dt1.isAfter(dt2);
if(check)
    System.out.println(dt1 +" is after "+dt2);
else
    System.out.println(dt2 +" is after "+dt1);

这篇关于比较两个日期与时间的甲酸盐是YYYY-MM-DD HH:MM:SS在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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