要检查日期是否在指定日期之后?

Pra*_*sav 5 java date

我正在尝试对日期进行验证,该日期应仅采用当前和未来日期,如果日期是旧日期则应显示

日期早于当天

我也想允许当前日期.现在正在给gievnDate作为当天,它总是显示

日期早于当天

但我期待输出为

日期是givenDate的未来日期.

以下是我一直在尝试的代码:

    Date current = new Date();
    String myFormatString = "dd/MM/yy";
    SimpleDateFormat df = new SimpleDateFormat(myFormatString);
    Date givenDate = df.parse("15/02/13");
    Long l = givenDate.getTime();
    //create date object
    Date next = new Date(l);
    //compare both dates
    if(next.after(current) || (next.equals(current))){
        System.out.println("The date is future day");
    } else {

        System.out.println("The date is older than current day");
    }
Run Code Online (Sandbox Code Playgroud)

在这里,当我在15/02/13检查时,它显示为比当前日期更早.我的方法错了吗?还是有更好的方法?

ass*_*ias 8

if(next.after(current) && (next.equals(current)))总是假的,因为nextcurrentAND等于同时不能严格current.

你可能意味着:

if(next.after(current) || (next.equals(current))) 用OR.


Bas*_*que 5

tl;博士

LocalDate                                      // Represents a date-only, without time-of-day and without time zone.
.parse( 
    "15/02/13" , 
    DateTimeFormatter.ofPattern( "dd/MM/uu" )  // Specify a format to match you're input.
)                                              // Returns a `LocalDate` object.
.isBefore(                                     // Compare one `LocalDate` to another.
    LocalDate.now(                             // Capture the current date…
        ZoneId.of( "America/Montreal" )        // …as seen through the wall-clock time used by the people of a particular region (a time zone).
    )                                          // Returns a `LocalDate` object.
)                                              // Returns a boolean.
Run Code Online (Sandbox Code Playgroud)

细节

关于您在布尔逻辑中的错误,其他答案是正确的。

此外,您正在使用麻烦的旧日期时间类,现在是遗留的,由 java.time 类取代。

上面提到的布尔逻辑使用类上的isBefore&isAfter方法更容易java.time.LocalDate

LocalDate

LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

时区对于确定日期至关重要。对于任何给定时刻,日期因地区而异。例如,在法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天” 。

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Run Code Online (Sandbox Code Playgroud)

DateTimeFormatter

要将输入字符串解析为日期时间值,请使用DateTimeFormatter该类。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uu" );
String input = "15/02/13";
LocalDate ld = LocalDate.parse( input , f );
Run Code Online (Sandbox Code Playgroud)

相比

要比较LocalDate对象,您可以调用诸如compareToequalsisAfterisBefore、 之类的方法isEqual

String message = "ERROR - Message not set. Error # f633d13d-fbbc-49a7-9ee8-bcd1cfa99183." ;
if( ld.isBefore( today ) ) {  // Before today.
   message = "The date: " + ld + " is in the past, before today: " + today );
} else if( ld.isEqual( today ) ) {  // On today.
   message = "The date: " + ld + " is today: " + today );
} else if( ld.isAfter( today ) ) {  // After today.
   message = "The date: " + ld + " is in the future, later than today: " + today );
} else {  // Double-check.
   message = "ERROR – Unexpectedly reached Case Else. Error # c4d56437-ddc3-4ac8-aaf0-e0b35fb52bed." ) ;
}
Run Code Online (Sandbox Code Playgroud)

格式

顺便说一句,缺少世纪的输入字符串的格式是不明智的。使用两位数字表示年份会使日期更难阅读,产生更多歧义,并使解析更加困难,因为各种软件在默认世纪时表现不同。我们现在可以负担得起现代计算内存和存储中的两位额外数字。

将日期时间值序列化为文本时,请坚持使用实用且流行的ISO 8601标准格式。对于仅限日期的 YYYY-MM-DD,例如2016-10-22.


关于 java.time

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

现在处于维护模式Joda-Time项目建议迁移到 java.time。

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

从哪里获得 java.time 类?

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多


And*_*s_D 3

除了阿西利亚斯指出的逻辑错误之外:

您没有指定时间,因此givenDate示例中的时间为“15/02/13 00:00:00.000”。现在,如果您将给定日期与同一天(又称“现在”)的任何真实时间戳进行比较,那么givenDate几乎总是“旧”然后“现在”。

设置givenDate为当天的最后毫秒,您将不需要“等于”测试。