前一年同一天(前一年,同一周和一周中的某一天)

Same day prevous year (previous year, same week and day of week)

我想检索去年的同一天。

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

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

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

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

这应该可以解决问题:

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);

您似乎希望上一年的日期与给定日期在一年中的同一周和同一天。下面的代码给你那个结果。

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

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

    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 没有星期天。

如果您正在寻找 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;
    }

}