2015-12-21 17 views
0

我有兩個dropdownlist,一個表示月份,另一個表示日期。在java中下拉列表中的幾天的可行算法

我一個月下拉列表如下,

public Map<Integer, String> month(){ 

    final Map<Integer, String> month = new LinkedHashMap<Integer, String>(); 
    month.put(4,"April"); 
    month.put(5,"May"); 
    month.put(6,"June"); 
    month.put(7,"July"); 
    month.put(8,"August"); 
    month.put(9, "September"); 
    month.put(10,"October"); 
    month.put(11,"November"); 
    month.put(12,"December"); 
    month.put(1, "January"); 
    month.put(2,"February"); 
    month.put(3, "March"); 

return monthmap; 
} 

天的地圖就像是

public Map<Integer, Integer> dayOfMonth(){ 

      Map<Integer, Integer> day = new LinkedHashMap<Integer, Integer>(); 
//In this section feasible algorithm required. 
      for(int i=1;i<32;i++) 
      { 
       day.put(i,i); 
      } 

      return day; 
     } 

基於此算法我31了每個月days.But我只需要正確的例如:「6月需要30天,在這種情況下,它有31天」。因此,我需要的是基於月份地圖,所有相應的日期應該插入到日期地圖中。

例如[(4,(1 to 30)),(5,(1 to 31)等高達行軍] .`

我使用Java核心爲這些部分code.For UI部分的jQuery和JavaScript是used.Spring MVC是used.I很努力的技術整整一天拿到fesiable solution.Any幫助將是非常可觀.....

+0

爲什麼您不計算月份中的#日期? – AsfK

+0

基於我的要求,我只需要添加到一天(我的列表)作爲鍵循環中所示的關鍵值對。 – Miller

回答

-1

查看日曆#getActualMaximum:https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getActualMaximum(int)

它可以讓你找到多少天都在每個月。

public static void main(String[] args) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.DAY_OF_MONTH, 1); 
    calendar.set(Calendar.MONTH, Calendar.FEBRUARY); 
    calendar.set(Calendar.YEAR, 2100); 

    System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28, not 29 
} 

@Arun訥格爾你的代碼是破碎的,不會對如工作。在2100年(可分爲4但不是閏年)。我不能downvote因爲我沒有足夠的聲譽,但我會

+0

嗨@spi,我會建議你提供一些示例代碼的鏈接,這將有助於OP,你會得到更好的來自其他用戶的反饋 –

+0

好的下次會做 – spi

2

你也不必在意閏年和其他...因此,創建一個Calendar,設置yearmonth和使用getActualMaximum要不要需要重新發明weel。

int iYear = 2016; 
int iMonth = Calendar.FEBRUARY; 
int iDay = 1; 

// Calendar object and set year and month 
Calendar cal = new GregorianCalendar(iYear, iMonth, iDay); 

// Number of days in that month 
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 29 !!! 

附加功能:

  • 在此之後,只要用daysInMonth + 1在實際for-loop

    for(int i=1; i < daysInMonth + 1; i++) 
    
  • 當然,更好地創造一個函數來封裝和更換2線

    private int getNumberOfDays(int month, int year) { 
        Calendar cal = new GregorianCalendar(year, month, 1); 
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 29 !!! 
    } 
    

    ,並調用它喜歡:getNumberOfDays(Calendar.FEBRUARY, 2016);

+0

其實我有兩個dropdownlists,一個用於表示月份和另一個我的下拉列表會在這些年中保持一致。無需在下一年更改它,等等。實際上它代表了學校的一個賽季。 – Miller

+0

2016年,2月將有29天,如果你想保持最低的質量,你必須**在你的應用中代表這一點......正如你所看到的,獲取日子的方法是2行代碼,好得多比每個月有12個固定值的地圖....你不覺得嗎? –

+0

好吧,我試試吧,會讓你知道 – Miller

1

java.time

在Java 8和更高版本,使用java.time框架。 Monthenum知道每個月的天數。

int daysInApril = Month.APRIL.maxLength(); 
int currentMonthLength = LocalDate.now().getMonth().maxLength(); 

如果需要,本年度天,獲得最新實例與LocalDate.now和使用他們的簡單plusMonthsminusMonths方法,直到你已經收集了全年的月份。然後就是使用相同的API獲取那些月的日子。

+1

您可能想省略2月29日的非[閏年](https://en.wikipedia.org/wiki /閏年)。如果特定年份是閏年,請詢問['Year'](http://docs.oracle.com/javase/8/docs/api/java/time/Year.html)課程。 'Year.of(2015).isLeap()'... or ...'Year.from(myLocalDate).isLeap()' –