2013-02-15 101 views
4

我想为此应采取只当前和未来日期,日期做验证,如果日期是较旧的日期,那么它应该显示检查日期是否在指定日期之后?

The date is older than current day

我想允许当前日期also.Right现在同时给予电流一天gievnDate,它总是呈现

The date is older than current day

但我期待的输出为

The date is future day for givenDate as today.

下面是我一直在努力的代码:

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"); 
    } 

这里的时候,我与15/2月13日确认,其表现为比当前day.Is年长我的方法不对或者有没有更好的办法?

+0

在当前*或*下一个等于当前(而不是*和*)之后,您的意思可能是下一个吗? – akaIDIOT 2013-02-15 10:21:53

+0

哪一行给出了哪个确切的异常?另外,请使这个独立的代码片段(添加任何相关的导入,或完全限定类名称,并将其放入类和方法中,以便运行)。 – 2013-02-15 10:23:29

回答

3

除了逻辑错误,该assylias指出:

你不指定时间,所以givenDate是“15/02/13 00:00:00.000”在你的例子。现在,如果您将该给定日期与当天的任何实时时间戳(又名“now”)进行比较,那么givenDate几乎总是“较旧”,然后“现在”。

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

8

if(next.after(current) && (next.equals(current)))总是错误的,因为next不能严格在current之后,并且等于current

你大概的意思是:

if(next.after(current) || (next.equals(current)))与OR。

2

此代码中至少有一个错误,但我不知道它是如何抛出异常的。声明:

if(next.after(current) && (next.equals(current))){ 

永远不会计算为true,因为据推测,next.after(current)next.equals(current)不都是真正的在同一时间。

你大概意思写:

if(next.after(current) || (next.equals(current))){ 

而且,它通常是一个好主意,至少包括错误的具体细节:在其确切的行不会例外抛出(如果那是真的什么你的意思),什么是确切的输出(即使输出是一个例外)给出了什么输入?

0

尽管这是一个古老的问题。
我想分享一个简单的解决方案。
您可以检查当前日期是否在下一个日期之前。 因为如果它不等于当前日期或当前日期之后。这就是你正在寻找的。 所以你不需要“或等于”检查。

if(!current.isBefore(next)){ 
    System.out.println("The date is future day"); 
}else{ 
    System.out.println("The date is older than current day"); 
} 

或者你甚至可以使用:

if(next.getTime() >= current.getTime()) 
2

TL;博士

LocalDate.parse("15/02/13" , DateTimeFormatter.ofPattern("dd/MM/uu")) 
    .isBefore(LocalDate.now(ZoneId.of("America/Montreal"))) 

详细

其他的答案是有关的布尔逻辑,你的错误是正确的。

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

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

LocalDate

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

时区对确定日期至关重要。对于任何特定的时刻,日期因地区而异。例如,Paris France午夜后几分钟是新的一天,而在Montréal Québec仍然是“昨天”。

ZoneId z = ZoneId.of("America/Montreal"); 
LocalDate today = LocalDate.now(z); 

DateTimeFormatter

为了解析的输入字符串作为日期时间值,使用DateTimeFormatter类。

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

比较

比较LocalDate对象,你可以调用的方法,如compareToequalsisAfterisBeforeisEqual

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.") ; 
} 

格式

顺便说一句,缺乏世纪的输入字符串的格式是不明智的。一年中使用两位数字会使日期难以阅读,造成更多歧义,并且使得解析更加困难,因为在违约世纪时各种软件的行为不同。现在我们可以在现代计算内存和存储中支付两位额外的数字。

将日期 - 时间值序列化为文本时,请使用实用且流行的ISO 8601标准格式。对于仅为约会的MMY-DD,如2016-10-22


关于java.time

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

Joda-Time项目现在位于maintenance mode,建议迁移到java.time。请参阅Oracle Tutorial。并搜索堆栈溢出了很多例子和解释。规格是JSR 310

从何处获取java.time类?

ThreeTen-Extra项目与其他类扩展java.time。这个项目是未来可能增加java.time的一个试验场。您可以在这里找到一些有用的类,如Interval,YearWeek,YearQuartermore

相关问题