如何存储日期,然后检查另一个日期是否与该日期匹配

时间:2013-10-28 01:46:51

标签: java

我正在努力弄清楚Java中的日期并且完全丢失了。我使用日期吗?使用纪元时间?格里高利历?

假设我想要存储日期,然后将其与其他日期进行比较。例如,我已存储日期“10/27/2013”​​。然后,我想稍后将其与稍后输入的日期进行比较,看看以后的日期是否与“10/27/2013”​​相同,或者只是日,年或月匹配?最好的方法是什么?

3 个答案:

答案 0 :(得分:1)

在Java中,java.util.Date类不仅仅是Date,它应该更准确地称为TimeStamp,因为它包含时间信息。

要与java.util.Date个实例进行比较,您需要编写一个忽略时间信息的自定义Comparator,或者我喜欢做的是删除时间信息。

以下是一些代码段,用于管理规范化任何java.util.Date对象,以便时间信息清零

所有这些都是名为DateUtil

的类的静态方法
public static boolean equalsIgnoreTime(final Date d1, final Date d2)
{
    return DateUtil.zeroTime(d1).equals(DateUtil.zeroTime(d2));
}

取决于以下静态方法:

public static Date zeroTime(final Date date)
{
    return DateUtil.setTime(date, 0, 0, 0, 0);
}

取决于:

public static Date setTime(final Date date, final int hourOfDay, final int minute, final int second, final int ms)
{
    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    gc.set(Calendar.HOUR_OF_DAY, hourOfDay);
    gc.set(Calendar.MINUTE, minute);
    gc.set(Calendar.SECOND, second);
    gc.set(Calendar.MILLISECOND, ms);
    return gc.getTime();
}

您对存储的引用不明确,因此我不知道如何解决您存储的方式或内容或存储数据的位置,但如果您正在使用Java进行比较代码,存储几乎无关紧要。

答案 1 :(得分:1)

与Java 7及更早版本捆绑在一起的日期和日历类非常糟糕。知情人士使用第三方开源库Joda-Time

Joda-Time具有在其对象和Java平台的Date类之间进行转换的方法。当您需要与期望Date实例的其他软件进行互操作时使用。

如果使用Java 8,请使用受Joda-Time启发的新JSR 310 Date and Time API。有些人试图将JSR 310实现反向移植到Java 7;我不知道他们是否成功了。

以下是使用Joda-Time演示您所询问的一些示例代码。使用风险自负;我没有彻底思考它,也没有测试过。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/

// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310

// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601

// Capture one moment in time.
org.joda.time.DateTime now = new org.joda.time.DateTime();
System.out.println("Now: " + now);

// Calculate approximately same time yesterday.
org.joda.time.DateTime yesterday = now.minusDays(1);
System.out.println("Yesterday: " + yesterday);

// Compare dates. A DateTime includes time (hence the name).
// So effectively eliminate the time by setting to start of day.
Boolean isTodaySameDateAsYesterday = now.withTimeAtStartOfDay().isEqual(yesterday.withTimeAtStartOfDay());
System.out.println("Is today same date as yesterday: " + isTodaySameDateAsYesterday);

org.joda.time.DateTime halloweenInUnitedStates = new org.joda.time.DateTime(2013, 10, 31, 0, 0); // Uses default time zone.
Boolean isFirstMomentSameDateAsHalloween = now.withTimeAtStartOfDay().isEqual(halloweenInUnitedStates.withTimeAtStartOfDay());
System.out.println("Is now the same date as Halloween in the US: " + isFirstMomentSameDateAsHalloween);

// Wait a moment.
try {
    java.util.concurrent.TimeUnit.MILLISECONDS.sleep(300);
} catch (InterruptedException e) {
    e.printStackTrace();
}

// See if new moment is on the same date as previous moment.
// May not be same date if we rolled over the stroke of midnight.
org.joda.time.DateTime aMomentLater = new org.joda.time.DateTime();
Boolean isFirstMomentSameDateAsAMomentLater = now.withTimeAtStartOfDay().isEqual(aMomentLater.withTimeAtStartOfDay());
System.out.println("Is first moment the same date as a moment later: " + isFirstMomentSameDateAsAMomentLater);

示例运行...

Now: 2013-10-27T20:36:21.406-07:00
Yesterday: 2013-10-26T20:36:21.406-07:00
Is today same date as yesterday: false
Is now the same date as Halloween in the US: false
Is first moment the same date as a moment later: true

从不将日期存储为您提到的文字:I've stored a date "10/27/2013".

尽可能将日期保留为日期对象,例如Joda-Time中的DateTime或数据库的本机日期时间格式。如果您必须serialize文本的日期时间,请使用可靠且可预测的格式,ISO 8601是明显的选择。该格式是Joda-Time中的默认格式,您可以在上面的示例运行中看到。

答案 2 :(得分:-2)

使用以下代码格式化数据库中的日期和存储。对新日期使用相同的格式并执行比较。

@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date date = null;