上一年的同一天(上一年,同一周和星期几)

时间:2019-06-30 09:15:37

标签: java date dayofweek week-number localdate

我想检索上一年的同一天。

例如今天是2019-03-30,即2019年,第26周(一年中的一周),第7天(一周中的一天)。

我需要构造LocalDate,即2018年,第26周(一年中的一周),第7天(一周中的一天)。

我无法从java.time软件包中找到可以像这样构建LocalDate的软件包。

4 个答案:

答案 0 :(得分:4)

您似乎希望将上一年的日期与年份的同一周和一周中的某天作为给定日期。下面的代码可以为您提供结果。

LocalDate currentLocalDate = LocalDate.now();
int dayOfWeek = currentLocalDate.getDayOfWeek().getValue();
int weekOfYear = currentLocalDate.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
LocalDate resultLocalDate = currentLocalDate
    .minusYears(1)
    .with(ChronoField.ALIGNED_WEEK_OF_YEAR, weekOfYear)
    .with(ChronoField.DAY_OF_WEEK, dayOfWeek);

完整示例(live copy):

import java.time.*;
import java.time.format.*;
import java.time.temporal.*;

class Example
{
    private static void showDateInfo(LocalDate ld) {
        int weekOfYear = ld.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
        int dayOfWeek = ld.getDayOfWeek().getValue();
        System.out.println(ld.format(DateTimeFormatter.ISO_LOCAL_DATE) + " is week " + weekOfYear + ", day " + dayOfWeek);
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        LocalDate currentLocalDate = LocalDate.of(2019, 6, 30);
        showDateInfo(currentLocalDate);
        int dayOfWeek = currentLocalDate.getDayOfWeek().getValue();
        int weekOfYear = currentLocalDate.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
        LocalDate resultLocalDate = currentLocalDate
            .minusYears(1)
            .with(ChronoField.ALIGNED_WEEK_OF_YEAR, weekOfYear)
            .with(ChronoField.DAY_OF_WEEK, dayOfWeek);
        showDateInfo(resultLocalDate);
    }
}

输出:

2019-06-30 is week 26, day 7
2018-07-01 is week 26, day 7

答案 1 :(得分:0)

我相信其他答案很接近,但还没有完全解决。据我了解,您想使用默认语言环境的星期方案,而其他答案则不这样做。我的建议是:

    WeekFields wf = WeekFields.of(Locale.getDefault());
    LocalDate today = LocalDate.now(ZoneId.of("Africa/Casablanca"));
    int week = today.get(wf.weekOfYear());
    int dow = today.get(wf.dayOfWeek());
    System.out.println("Today is " + today + ", "
            + today.getDayOfWeek() + " of week " + week);

    LocalDate correspondingDayLastYear = today.minusYears(1)
            .with(wf.weekOfYear(), week)
            .with(wf.dayOfWeek(), dow);
    System.out.println("Last year was " + correspondingDayLastYear + ", "
            + correspondingDayLastYear.getDayOfWeek()
            + " of week " + correspondingDayLastYear.get(wf.weekOfYear()));

在我的计算机上运行时,输出为:

Today is 2019-06-30, SUNDAY of week 26
Last year was 2018-07-01, SUNDAY of week 26

当我将地区设置为美国时,我得到的日期是相同的,但是由于美国周的定义不同,所以周号也不同:

Today is 2019-06-30, SUNDAY of week 27
Last year was 2018-07-01, SUNDAY of week 27

我相信在某些情况下,不同的语言环境也会给您不同的日期。

wf.dayOfWeek()为您提供了一个字段,该字段根据该特定语言环境中的星期几从1到7进行编号。重要的是,不仅要使用withDayOfWeek或同等的货币,否则,如果不使用ISO周,您可能会滑入另一个周。

仍然,我的答案将永远无法正常工作!如果今天在一年的第53周之内,则很可能是去年没有第53周。我们对此无能为力。我们无法解决的另一个问题:在美国的一周中,第1周从1月1日开始。如果这是星期日,则是该周的第一天,但​​随后一年的第1周从星期五或周六开始,因此1没有任何星期天。

答案 2 :(得分:0)

如果您正在寻找与 ISO 周年兼容的函数,这对我有用 - 到目前为止 =]。

public class FiscalDateUtil {
    
    private static Logger log = LoggerFactory.getLogger(FiscalDateUtil.class);

    static public LocalDate previousYearComparisonDate(LocalDate date) {
    
        final int weekYear = date.get(IsoFields.WEEK_BASED_YEAR);
        final int weekLastYear = weekYear-1;
        
        final DayOfWeek dayOfWeek = date.getDayOfWeek();
        final int weekOfYear = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
        final LocalDate adjustedLastYear = date
                .with(IsoFields.WEEK_BASED_YEAR, weekLastYear)
                .with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, weekOfYear)
                .with(TemporalAdjusters.nextOrSame(dayOfWeek));

        if (log.isTraceEnabled()) {
            log.trace("starting date {}", date.toString());
            log.trace("starting date dow {}", date.getDayOfWeek());
            log.trace("lastYear {}", weekLastYear);
            log.trace("lastYear dow {}", dayOfWeek);
            log.trace("adjustedLastYear {}", adjustedLastYear.toString());
            log.trace("adjustedLastYear dow {}", adjustedLastYear.getDayOfWeek());  
        }
        return adjustedLastYear;
    }

}

答案 3 :(得分:-1)

您可以将自当前日期的年初以来的天数添加到前一年的年初。

这应该可以解决问题:

String dateStr = "2019-03-30";
LocalDate date = LocalDate.parse(dateStr);
LocalDate newDate = LocalDate.parse(date.getYear() + "-01-01").plusDays(date.getDayOfYear());
System.out.println(newDate);