获取星期的开始日期(可配置的开始星期几)

时间:2013-07-08 07:13:47

标签: java

我有当前日期,以及一个常数,它告诉星期从哪一天开始。我想根据该常数得到一周的开始日期。如果我将一周的第一天硬编码到星期一(或任何东西),那么这很简单。但是一周的第一天不断变化。因此,每次要更改第一天时,我都不想更改代码。

这是我用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 个答案:

答案 0 :(得分:2)

来自java calendar 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:

答案 1 :(得分:1)

我使用了以下方法:

/** 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();
}

答案 2 :(得分: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" ) )

请参阅此code run live at IdeOne.com

  

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

避免遗留类

你正在使用现在遗留下来的麻烦的旧日期时间类,取而代之的是java.time类。

DayOfWeek

不要仅使用整数来表示代码中的星期几,而是使用Java内置的DayOfWeek enum。这样可以提高类型安全性,确保有效值,并使代码更具自我记录性。

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.

答案 3 :(得分:0)

DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate now = LocalDate.now();
String  startDate = now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).format(format);