3

我有当前日期和一个常数,它告诉一周从哪一天开始。我想根据该常数获取一周的开始日期。如果我将一周的第一天硬编码到星期一(或其他任何时间),那么这很简单。但是一周的第一天一直在变化。所以不想改代码,每次第一天都要改。

这是我用java的日历尝试过的:

public static Date getLastWeekdayDate()
{
    Calendar calendar = new GregorianCalendar();
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
    int daysToSubtractFromCurrentDate = 0;

    switch (dayOfWeek)
    {
    case Calendar.WEDNESDAY:
        daysToSubtractFromCurrentDate = 4;
        break;

    case Calendar.THURSDAY:
        daysToSubtractFromCurrentDate = 5;
        break;

    case Calendar.FRIDAY:
        daysToSubtractFromCurrentDate = 6;
        break;

    case Calendar.SATURDAY:
        daysToSubtractFromCurrentDate = 0;
        break;

    case Calendar.SUNDAY:
        daysToSubtractFromCurrentDate = 1;
        break;

    case Calendar.MONDAY:
        daysToSubtractFromCurrentDate = 2;
        break;

    case Calendar.TUESDAY:
        daysToSubtractFromCurrentDate = 3;
        break;
    }

    calendar.add(Calendar.DATE, -daysToSubtractFromCurrentDate);
    calendar.set(Calendar.AM_PM, Calendar.AM);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    return calendar.getTime();
}

我想得到一周的开始日期。上面的函数返回一周的第一天,一周的开始日期被硬编码为星期六。每当关于一周开始日的要求发生变化时,我都必须更改代码。

4

4 回答 4

2

来自 java 日历 API http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#getFirstDayOfWeek()

public int getFirstDayOfWeek()
Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.
Returns:
the first day of the week.
See Also:
于 2013-07-08T07:31:45.160 回答
2

我使用了以下方法:

/** 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday,
* 6 = Friday, 7 = Saturday
*/
public static Date getFirstDayOfWeekDate(int firstDay)
{
    // Calculate the date of the first day of the week

    // First get the today's date
    Calendar c = new GregorianCalendar();

    // Now set the day of week to the first day of week
    while (c.get(Calendar.DAY_OF_WEEK) != firstDay)
    {
        c.add(Calendar.DAY_OF_MONTH, -1);
    }

    return c.getTime();
}
于 2013-07-16T19:08:00.400 回答
0

tl;博士

LocalDate.now( ZoneId.of( "America/Montreal" ) )
         .with( TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) )  // Specify your desired `DayOfWeek` as start-of-week.
         .atStartOfDay( ZoneId.of( "America/Montreal" ) )

请参阅在 IdeOne.com 上实时运行的代码

zdt: 2017-07-09T00:00-04:00[美国/蒙特利尔] | 星期几:星期日

避免遗留类

您正在使用麻烦的旧日期时间类,它们现在是遗留的,被 java.time 类所取代。

DayOfWeek

与其在代码中仅使用整数来表示星期几,不如使用Java 中内置的枚举。这可以获得类型安全,确保有效值,并使您的代码更具自我记录性。DayOfWeek

DayOfWeek weekStart = DayOfWeek.SUNDAY ;  // Pass whatever `DayOfWeek` object you want.

TemporalAdjuster&LocalDate

TemporalAdjuster接口支持操作日期以获取另一个日期的方法。在课堂上找到一些实现TemporalAdjusters(注意复数)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
LocalDate start = today.with( TemporalAdjusters.previousOrSame( weekStart ) ) ;  

ZonedDateTime

要获得准确的时刻,请询问LocalDate当天的第一刻。那一刻取决于时区,因为全球任何特定时刻的日期都不同。

ZonedDateTime zdt = start.atStartOfDay( z ) ;

Instant

如果您想以 UTC 格式查看某个时刻,请提取一个Instant对象。

Instant instant = zdt.toInstant() ;  // Same moment, different wall-clock time.
于 2017-07-10T19:56:40.693 回答
0
DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate now = LocalDate.now();
String  startDate = now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).format(format);
于 2020-02-13T05:12:42.957 回答