Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何存储日期,然后检查另一个日期是否与该日期匹配_Java - Fatal编程技术网

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

Java 如何存储日期,然后检查另一个日期是否与该日期匹配,java,Java,我试图用Java计算日期,但我完全不知所措。我用日期吗?使用大纪元时间?公历 假设我想存储一个日期,然后将其与其他日期进行比较。例如,我存储了一个日期“10/27/2013”。然后,我想稍后将其与稍后输入的日期进行比较,以查看稍后的日期是否与“10/27/2013”相同,或者是否只是日期、年份或月份匹配?最好的方法是什么?在Java中,Java.util.Date类不仅仅是一个Date,它应该更准确地称为时间戳,因为它包含时间信息 要与java.util.Date实例进行比较,您需要编写一个忽略

我试图用Java计算日期,但我完全不知所措。我用日期吗?使用大纪元时间?公历


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

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

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

下面是一些代码片段,用于管理任何
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代码进行比较,则存储非常不相关。

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

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

与Java7和更早版本捆绑在一起的Date和Calendar类是出了名的糟糕。知情者使用第三方开源库

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

如果使用Java8,请使用受Joda Time启发的新版本。一些人试图将JSR310实现移植到Java7;我不知道他们是否成功了

下面是相同的示例代码,使用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

永远不要像您提到的那样将日期存储为文本:
我存储了一个日期“10/27/2013”。


只要有可能,将日期作为日期对象保留,例如Joda Time中的
DateTime
,或作为数据库的本机日期时间格式。如果您必须输入日期-时间文本,请使用可靠且可预测的格式,这是显而易见的选择。该格式是Joda Time中的默认格式,正如您在上面的run示例中所看到的。

它取决于您要检查的级别。对你来说,什么是匹配的日期?另外,您尝试了什么?我建议您签出。Java
Date
是一个容器,用于表示自Unix时代以来的毫秒数。除非您特别需要
Calendar
,否则使用默认的
Calendar
Calendar.getInstance
)应该足以进行操作。日期比较可以使用
Date#equals
Date#before
Date#before
进行,但这包括
日期的时间值
,因此将
日期
对象格式化为
字符串可能会产生更好的结果