简体   繁体   English

仅比较日期(而不是时间)

[英]Comparing Just the Date (not time)

I need to compare 2 dates to a third date and ignore the time portion of all of them. 我需要将2个日期与第三个日期进行比较,并忽略所有日期的时间部分。

The code below generates a parse exception because the toString() method returns something like "Wed Feb 26 00:00:00 EST 2014". 下面的代码生成一个解析异常,因为toString()方法返回的内容类似于“ EST 2014年2月26日星期三00:00:00”。

Any suggestions on how I might fix this? 关于如何解决此问题的任何建议?

private boolean needToSendEmail(EmSelfCertEntity escd) throws ParseException {
        boolean sendEmail = false;

        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

        Date justTheDate = df.parse(escd.getCurrentFCESDate().toString());
        Calendar firstSent = Calendar.getInstance();
        firstSent.setTime(justTheDate);

        justTheDate = df.parse(new Date().toString());
        Calendar firstFollowUp = Calendar.getInstance();
        firstFollowUp.setTime(justTheDate);
        firstFollowUp.add(Calendar.DATE, -daysToFirstFollowUpEmail);

        Calendar secondFollowUp = Calendar.getInstance();
        secondFollowUp.setTime(justTheDate);
        secondFollowUp.add(Calendar.DATE, -daysToSecondFollowUpEmail);

        if ((firstSent.before(firstFollowUp) && escd.countEmailsSent <= 1)
                || (firstSent.before(secondFollowUp) && escd.countEmailsSent <= 2)) {

            sendEmail = true;
        }
    return sendEmail;
}

Thanks! 谢谢!

Why are you parsing the String when you already have the Date? 为什么已经有日期时为什么要解析字符串?

If you want to format your existing Date into the format you specified, use the format() method instead: 如果要将现有的Date格式化为指定的格式,请改用format()方法:

String justTheDate = df.format(new Date());

Then you can compare the Strings using the equals() method to check for matches. 然后,您可以使用equals()方法比较字符串以检查是否匹配。

Edit- By the way, if Java 8 is an option (it came out on Tuesday!), its new DateTime features will do exactly what you're looking for: http://docs.oracle.com/javase/tutorial/datetime/ 编辑-顺便说一下,如果可以选择使用Java 8(星期二发布!),它的新DateTime功能将完全满足您的需求: http : //docs.oracle.com/javase/tutorial/datetime /

The cause for your exception is that toString in escd.getCurrentFCESDate().toString() delivers another format than "MM/dd/yyyy". 导致您异常的原因是, escd.getCurrentFCESDate().toString()提供了另一种格式,而不是“ MM / dd / yyyy”。 So make sure that either your format String in line SimpleDateFormat("MM/dd/yyyy") is correct. 因此,请确保您在SimpleDateFormat(“ MM / dd / yyyy”)行中的格式字符串正确。 Or check if you can get the year, month, and day directly from getCurrentFCESDate(). 或者检查是否可以直接从getCurrentFCESDate()获取年份,月份和日期。

Just Use the calendar to create a date, where you take the year, months, day from the existing date but set the hours, minutes, seconds and millis to zero. 只需使用日历创建一个日期,即可从现有日期中获取年,月,日,但将小时,分钟,秒和毫秒设置为零。
The result will be a Date object: 结果将是一个Date对象:

Something like 就像是

firstSent.set(Calendar.HOUR, 0);
firstSent.set(Calendar.MINUTE, 0);
firstSent.set(Calendar.SECOND, 0);
firstSent.set(Calendar.MILLISECOND, 0);

Then use before() and after() 然后使用before()和after()

The easiest approach would be to convert the dates to numbers in this format: yyyyMMdd . 最简单的方法是将日期转换为以下格式的数字: yyyyMMdd

And after that you can just compare the numbers. 之后,您可以比较数字。

But yes, please work with timezone adjustments before converting to numbers. 但是,是的,请先进行时区调整,然后再转换为数字。

you can calculate the time in millis and substract the time with a simple / division. 您可以以毫秒为单位计算时间,并用简单的/除法来减去时间。 This way you can compare 2 longs and check if one is bigger than another. 这样,您可以比较2个多头并检查一个多头。

Take this example where we get to different dates for today (500 milliseconds from one to another) but... if you divide by 86400000 then... you get the same number. 以这个例子为例,我们今天到达不同的日期(一个日期到另一个日期为500毫秒),但是...如果除以86400000,那么...您将得到相同的数字。

Try this: 尝试这个:

    public static void main(String[] args) throws Exception {
    Date d1= new Date();

    Thread.sleep(500);
    Date d2= new Date();

    final int MILLISECONDS = 1000;
    final int SECONDS = 60;
    final int MINUTES = 60;
    final int HOURS = 24;

    final long MILLI_PER_DAY= MILLISECONDS*SECONDS*MINUTES*HOURS;
    System.out.println(MILLI_PER_DAY);

    System.out.println(d1.getTime());
    System.out.println(d2.getTime());
    System.out.println(d1.getTime()/MILLI_PER_DAY);
    System.out.println(d2.getTime()/MILLI_PER_DAY);
}

You will see that the last 2 entries are the same: 您将看到最后两个条目相同:

1395338535623 --> time 1 in millis 1395338536123 --> time 2 in millis 16149 --> time 1 / 86400000 16149 --> time 2 / 86400000 --> THE SAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM