2014-02-08 133 views

回答

2
/** 
    * 
    * @param startDate 
    * @param endDate 
    * @return Start Date and End Date are <b>Inclusive</b>, days returned between these two dates 
    */ 
    protected List<String> getWeekDayNames(Date startDate, Date endDate) { 
     List<String> days = new ArrayList<String>(); 

     Calendar startCal = Calendar.getInstance(); 
     startCal.setTime(startDate); 

     Calendar endCal = Calendar.getInstance(); 
     endCal.setTime(endDate); 

     if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) { 

      days.add(this.formatDayOfWeek(startCal.getTime())); 

      return Collections.unmodifiableList(days); 
     } 
     // swap values 
     if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) { 
      startCal.setTime(endDate); 
      endCal.setTime(startDate); 
     } 

     do { 

      days.add(this.formatDayOfWeek(startCal.getTime())); 

      startCal.add(Calendar.DAY_OF_MONTH, 1); 

     } while (startCal.getTimeInMillis() <= endCal.getTimeInMillis()); 

     return Collections.unmodifiableList(days); 
    } 

用法:

Calendar cal = Calendar.getInstance(); 
     cal.add(Calendar.DAY_OF_MONTH, 15); 

     List<String> list = new Test().getWeekDayNames(new Date(), cal.getTime()); 

     System.out.println(list); 

輸出:

[SATURDAY, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY] 
1

喬達時間

通常我會建議Joda-Time庫,一個流行的replacemen t用於臭名昭着的java.util.Date &與Java捆綁在一起的java.util.Calendar類。但該問題不需要第三方庫。

java.time。*

所以,與其喬達時間,我的代碼下面的例子使用了與Java 8捆綁在一起的新java.time.* package。這些課程受Joda-Time的啓發,但完全重新設計。它們由JSR 310定義。有關更多信息,請參閱Oracle的new Tutorial

該解決方案非常簡單。歸結到這個單行的片段...

DayOfWeek.from(zonedDateTime).getDisplayName(TextStyle.FULL, Locale.US); 

爲了好玩,我扔了一個額外的行來顯示它是多麼容易本地化。在這種情況下,我會顯示法語以及美國英語單詞的星期幾。

以下是整個代碼段,如果您導入java.time.*java.time.format.*,則準備運行。

ZoneId timeZone = ZoneId.of("America/New_York"); 

ZonedDateTime start = ZonedDateTime.now(timeZone); 
ZonedDateTime stop = start.plusDays(2); 

// Usually spans of time are handled in a "half-open" manner, meaning start is inclusive and stop is exclusive. 
// But the Question required both start and stop to be inclusive. So add "1". 
long days = java.time.temporal.ChronoUnit.DAYS.between(start, stop) + 1L; 

System.out.println(days + " days from " + start + " to " + stop + " inclusive…"); 
for (int i = 0; i < days; i++) { 
    ZonedDateTime zonedDateTime = start.plusDays(i); 
    String dayOfWeek = DayOfWeek.from(zonedDateTime).getDisplayName(TextStyle.FULL, java.util.Locale.US); 
    String dayOfWeek_Français = DayOfWeek.from(zonedDateTime).getDisplayName(TextStyle.FULL, java.util.Locale.FRENCH); 
    System.out.println("zonedDateTime: " + zonedDateTime + " dayOfWeek: " + dayOfWeek + " dayOfWeek_Français: " + dayOfWeek_Français); 
} 

當運行...

3 days from 2014-02-08T06:06:33.335-05:00[America/New_York] to 2014-02-10T06:06:33.335-05:00[America/New_York] inclusive… 
zonedDateTime: 2014-02-08T06:06:33.335-05:00[America/New_York] dayOfWeek: Saturday dayOfWeek_Français: samedi 
zonedDateTime: 2014-02-09T06:06:33.335-05:00[America/New_York] dayOfWeek: Sunday dayOfWeek_Français: dimanche 
zonedDateTime: 2014-02-10T06:06:33.335-05:00[America/New_York] dayOfWeek: Monday dayOfWeek_Français: lundi 
相關問題