繁体   English   中英

Android获取两个日期之间的星期几名称

[英]Android get week day names between two dates

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

我想获得周六,周日,周一等两天之间的名字。

/**
     * 
     * @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]

乔达时间

通常,我会建议使用Joda-Time库,它是Java捆绑的臭名昭著的java.util.Date和java.util.Calendar类的流行替代品。 但是问题不需要第三方库。

java.time。*

因此,下面的代码示例使用了与Java 8捆绑在一起的新java.time。*包 ,而不是Joda-Time。 这些课程均受Joda-Time的启发,但已完全重新设计。 它们由JSR 310定义。 有关更多信息,请参见Oracle的新教程

解决方案非常简单。 归结为这一单行片段…

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM