2016-01-23 57 views
2
年什麼週六或週日

我不知道我怎麼會去檢查,看看哪個週六或週日就在這一年(2016)如何檢查它是基於Java

例如,讓我們說,如果用戶輸入日期2016/01/23,它會告訴用戶它是今年的第四個星期六。

另外,如何檢查以確保用戶在星期六或星期天進入,即如果用戶沒有返回週六或週日的有效日期,它會要求他們再次輸入輸入年份

感謝您的幫助!

+0

您使用哪種編程語言?你還嘗試過什麼嗎? – ACarter

+0

Java,使用SimpleDateFormat類 – Tom

+0

可能的重複[如何通過傳遞特定日期來確定星期幾?](http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week- by-passing-specific-date) – rkosegi

回答

0

在Java中使用的特別好的庫是Joda Time。它有能力進行各種間隔計算,以便您可以確定一年中的某個東西是否是七的倍數 - 這正是您實際需要的。

您將輸入日期作爲org.joda.time.DateTime,然後從那裏使用dayOfYear()計算一年中的某天。從那裏確定它是星期六還是星期日是一個簡單的計算。您只需獲取第一個週六和週日的DateTime.dayOfYear,然後檢查天數差異是否爲7的倍數:if((dayOfYearInput - dayOfYearFirst) % 7 == 0)。如果您的dayOfYearFirst在第一個星期六或星期天的這個表達式的計算結果爲真,那麼您知道這是星期六或星期天,否則它是不兼容的日期。

要檢查週六或週日是,你只需要使用(dayOfYearInput - dayOfYearFirst)/7 + 1,這將給你一個序,其中週六/週日是

同樣的方法可以用java.util.Calendar#get(Calendar.DAY_OF_YEAR) BTW使用 - 但喬達例如,如果您想堅持數據庫,時間特別有用。

但是,如果您希望自動計算一年中的第一個週六和週日,則您需要遍歷一年中的前七個日期:xxxx-01-01至xxxx-01-07,並確定無論是星期六還是星期天的dayOfWeek,並從那裏應用以前的邏輯。在java.util.Calendar中,您可以使用get(Calendar.DAY_OF_WEEK)

4

以下是兩個版本,演示如何檢查日期是星期六還是星期天,以及如何計算年的工作日。

使用Calendar(所有的Java版本):

private static void testUtilDate(String dateString) throws ParseException { 
    System.out.println(dateString); 
    java.util.Date date = new SimpleDateFormat("yyyy/MM/dd").parse(dateString); 
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(date); 

    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); 
    if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) { 
     System.out.println(" It is a Saturday or Sunday"); 
    } else { 
     System.out.println(" It is NOT a Saturday or Sunday"); 
    } 

    int dayOfWeekInMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); 
    System.out.println(" Day-of-Week in Month: " + dayOfWeekInMonth); 

    int dayOfWeekInYear = ((cal.get(Calendar.DAY_OF_YEAR) - 1)/7) + 1; 
    System.out.println(" Day-of-Week in Year: " + dayOfWeekInYear); 
} 

使用LocalDate(Java的8+):

private static void testLocalDate(String dateString) { 
    System.out.println(dateString); 
    java.time.LocalDate date = java.time.LocalDate.parse(dateString); 

    DayOfWeek dayOfWeek = date.getDayOfWeek(); 
    if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) { 
     System.out.println(" It is a Saturday or Sunday"); 
    } else { 
     System.out.println(" It is NOT a Saturday or Sunday"); 
    } 

    int dayOfWeekInMonth = (date.getDayOfMonth() - 1)/7 + 1; 
    System.out.println(" Day-of-Week in Month: " + dayOfWeekInMonth); 

    int dayOfWeekInYear = (date.getDayOfYear() - 1)/7 + 1; 
    System.out.println(" Day-of-Week in Year: " + dayOfWeekInYear); 
} 

測試

testUtilDate("2016/01/23"); 
testLocalDate("2016-01-23"); 
testUtilDate("2016/07/04"); 
testLocalDate("2016-07-04"); 

輸出

2016/01/23 
    It is a Saturday or Sunday 
    Day-of-Week in Month: 4 
    Day-of-Week in Year: 4 
2016-01-23 
    It is a Saturday or Sunday 
    Day-of-Week in Month: 4 
    Day-of-Week in Year: 4 
2016/07/04 
    It is NOT a Saturday or Sunday 
    Day-of-Week in Month: 1 
    Day-of-Week in Year: 27 
2016-07-04 
    It is NOT a Saturday or Sunday 
    Day-of-Week in Month: 1 
    Day-of-Week in Year: 27 
+0

不錯的代碼,但他需要在今年的週六/週日的序數 - 也許根據我的解決方案在下面調整? –

+0

@MrRho「年中的星期幾」*是*年中Xxxday的序數,無論Xxxday是星期六,星期日,星期一,......還是星期五。 – Andreas

+0

謝謝@Andreas,你每天都會學到新東西;) –

0

你可以試試這個:

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 

public class Temp { 

    public static void main(String[] args) { 
     Date date = null; 
     String dateString = "23/01/2016"; 
     try { 
      date = new SimpleDateFormat("dd/mm/yyyy").parse(dateString); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     Calendar c = Calendar.getInstance(); 
     c.setTime(date); 
     String[] dayOfWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; 
     String day = dayOfWeek[c.get(Calendar.DAY_OF_WEEK) - 1]; 
     int dayOfYear = c.get(Calendar.DAY_OF_YEAR)/7 + 1; 
     System.out.println(dateString + " happens to be the #" + dayOfYear + " " + day + " of year."); 
    } 
} 

上述代碼的輸出:

23/01/2016 happens to be the #4 Saturday of year.

對於給定的日期,它會打印在這一天發生數那一年。 希望這是你在問什麼。

0
public static void isValidDate(String date) throws ParseException { 
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(new SimpleDateFormat("yyyy/M/dd").parse(date)); 
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); 

    String result = (dayOfWeek == Calendar.SATURDAY) ? "Saturday" : (dayOfWeek == Calendar.SUNDAY) ? "Sunday" : null; 

    if (result == null) { 
     System.out.println("Not a valid date "); 
    } else { 
     System.out.println("Valid date "); 
     int dayOfWeekInMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); 
     System.out.println("The Date is " + dayOfWeekInMonth + "th "+result+" of the Week in the Month"); 
     int dayOfWeekInYear = ((cal.get(Calendar.DAY_OF_YEAR) - 1)/7) + 1; 
     System.out.println("The date is in " + dayOfWeekInYear + "th week of the Year"); 

    } 
} 
+0

不解決問題。 *全年*之前有多少星期六或星期日。我建議修復或刪除這個答案。 –

相關問題