2017-10-08 56 views
0

我正在構建一個應用程序,需要獲取本月具有dueDate(填寫表單)的用戶列表 - 然後將這些用戶過濾爲那些在dueDate周。Java - 一週內的布爾值

所以

if(isWithinAWeek(dueData)){ 
//alarm - 1 
} 
else{ 
//alarm - 0 
} 

---我需要幫助確保這個布爾isWithinAWeek方法正確

public static Boolean isWithinAWeek(Date date1){ 
    Date c1 = setTimeInDays(date1, 7);  
    if(c1.after(date1)){ 
     return true; 
    } 
    return false; 
} 


public static Date setTimeInDays(Date startingTime, Integer days){ 
    Calendar c = Calendar.getInstance(); 
    c.setTime(startingTime); 
    c.add(Calendar.DATE, days);//a day in the future or past 
    return c.getTime(); 
} 
+0

檢查代碼是否正確的最好方法是編寫單元測試用例和/或向其傳遞不同的值並檢查。如果事情不能按預期工作,請讓我們知道:) – Nishit

回答

1

因爲你寫它永遠是真實的條件。 它更容易看到,如果我把它簡化:

public static boolean isWithinAWeek(Date date) { 
    return setTimeInDays(date, 7).after(date); 
} 

這相當於你的,只是短。 正如你所看到的,它有效地說「如果日期+ 7天是在日期之後返回true」。 這總是如此。

您要檢查+7天是後如果現在date, 不date + 7天:

public static boolean isWithinAWeek(Date date) { 
    return setTimeInDays(new Date(), 7).after(date); 
} 

我也改變了返回類型從Booleanboolean, 因爲值永遠不會爲空。 我建議將setTimeInDays重命名爲plusDays, ,因爲這將類似於最近版本的Java 中的現代日期API(因此在您學習它時看起來很熟悉)。

0

您可以使用Period來計算這個很容易。這裏有一個例子:

public static boolean isWithinAWeek(LocalDate dueDate) { 
    boolean result = false; 
    LocalDate today = LocalDate.now(); 
    System.out.println("Today is " + today); 
    System.out.println("Due Date is " + dueDate); 

    Period p = Period.between(today, dueDate); 

    System.out.println("The period between these two is " + p.getYears() + " years, " + p.getMonths() +" months, " + p.getDays() + " days."); 

    // do you want to measure a week backwards? If so, change "p.getDays() >= 0" to "p.getDays() >= -7" 
    if (p.getYears() == 0 && p.getMonths() == 0 && p.getDays() >= 0 && p.getDays() <= 7) { 
     System.out.println(String.format(" └─ Yes, %1$s is within a week from %2$s.", dueDate, today)); 
     result = true; 
    } else { 
     System.out.println(String.format(" └─ No, %1$s is NOT within a week from %2$s.", dueDate, today)); 
     result = false; 
    } 

    return result; 
} 

以下方式調用此:

public static void main(String[] args) { 

    LocalDate due; 

    // 7 days from now 
    due = LocalDate.of(2017, Month.OCTOBER, 15); 
    System.out.println(isWithinAWeek(due)); 

    System.out.println(); 

    // 8 days from now 
    due = LocalDate.of(2017, Month.OCTOBER, 16); 
    System.out.println(isWithinAWeek(due)); 

    System.out.println(); 

    // a month from now 
    due = LocalDate.of(2017, Month.NOVEMBER, 8); 
    System.out.println(isWithinAWeek(due)); 

    System.out.println(); 

    // 5 days ago 
    due = LocalDate.of(2017, Month.OCTOBER, 3); 
    System.out.println(isWithinAWeek(due)); 

} 

將產生如下:

Today is 2017-10-08 
Due Date is 2017-10-15 
The period between these two is 0 years, 0 months, 7 days. 
    └─ Yes, 2017-10-15 is within a week from 2017-10-08. 
true 

Today is 2017-10-08 
Due Date is 2017-10-16 
The period between these two is 0 years, 0 months, 8 days. 
    └─ No, 2017-10-16 is NOT within a week from 2017-10-08. 
false 

Today is 2017-10-08 
Due Date is 2017-11-08 
The period between these two is 0 years, 1 months, 0 days. 
    └─ No, 2017-11-08 is NOT within a week from 2017-10-08. 
false 

Today is 2017-10-08 
Due Date is 2017-10-03 
The period between these two is 0 years, 0 months, -5 days. 
    └─ No, 2017-10-03 is NOT within a week from 2017-10-08. 
false 
0

java.time

您正在使用舊麻煩日期現在已經成爲傳統的類,被行業領先的java.time類取代。

如果與其他代碼交互給出java.util.Date,則使用添加到舊類的新方法轉換爲Instant

Instant instant = myJavaUtilDate.toInstant() ; 

對於僅限日期的值,請使用LocalDate類。

您的代碼忽略了時區的關鍵問題。如果你沒有另外指定,Java隱式地使用你的JVM的當前默認時區。最好是明確的。

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

提取僅限日期的值,指定時區以確定該日期。請記住,在任何特定時刻,全球各地的時間和日期因地區而異。

ZonedDateTime zdt = instant.atZone(z) ; 
LocalDate target = zdt.toLocalDate() ; 

獲取今天的日期,以及下週和下月。

LocalDate today = LocalDate.now(z) ; 
LocalDate nextWeek = today.plusDays(7) ; 
LocalDate nextMonth = today.plusMonths(1) ; 

比較。

Boolean isInNextWeek = (! target.isBefore(today)) && target.isBefore(nextWeek) ; 
Boolean isInNextMonth = (! target.isBefore(today)) && target.isBefore(nextMonth) ;