Android在两个日期之间获取工作日名称

时间:2014-02-08 07:33:35

标签: java android date dayofweek difference

我希望使用简单的Java在两个日期之间获取日期名称,而不使用任何第三方库。

我希望得到周六,周日,周一等两天的名字。

2 个答案:

答案 0 :(得分: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 :(得分:1)

约达时间

通常我会建议使用Joda-Time库,这是臭名昭着的java.util.Date&amp; amp;与Java捆绑在一起的java.util.Calendar类。但该课题不需要第三方图书馆。

java.time。*

因此,我的代码示例不是使用Joda-Time,而是使用与java.time.* package捆绑在一起的新Java 8。这些课程的灵感来自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