我试图找出Java中的日期并且完全丢失。我是否使用日期?使用纪元时间?公历?如何存储日期,然后检查另一个日期是否与该日期匹配
比方说,我有一个想要存储日期,然后再比较它与其他日期。例如,我存储了一个日期“10/27/2013”。然后,我想稍后将其与稍后输入的日期进行比较,以查看后续日期是否与“2013年10月27日”相同,或者只是日期,年份或月份是否匹配?什么是最好的方法来做到这一点?
我试图找出Java中的日期并且完全丢失。我是否使用日期?使用纪元时间?公历?如何存储日期,然后检查另一个日期是否与该日期匹配
比方说,我有一个想要存储日期,然后再比较它与其他日期。例如,我存储了一个日期“10/27/2013”。然后,我想稍后将其与稍后输入的日期进行比较,以查看后续日期是否与“2013年10月27日”相同,或者只是日期,年份或月份是否匹配?什么是最好的方法来做到这一点?
在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();
}
你引用存储 a不清楚,所以我不知道如何解决你存储或存储数据的方式或存储方式,但是如果你正在使用Java代码进行比较,那么存储是非常不相关的。
使用下面的代码格式化日期并存储在数据库中。对新日期使用相同的格式并进行比较。
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date date = null;
与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中的默认格式,如上面示例中所示。
这取决于你想检查的水平。对你来说,什么是一个匹配的日期?另外,你有什么尝试? –
我会推荐查看[JodaTime](http://www.joda.org/joda-time/)。 – atomman
Java'Date'是一个自Unix纪元以来的毫秒数的容器。除非您特别需要'Calendar',否则使用默认的'Calendar'('Calendar.getInstance')应该足以进行操作。日期比较可以使用'Date#equals','Date#before'和'Date#after'来完成,但是这包括Date的时间值,所以将Date对象格式化为String可能会产生更好的结果 – MadProgrammer