java 在java中获取下周和上周的开始和结束日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10465487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:09:53  来源:igfitidea点击:

Get next week and previous week staring and ending dates in java

javadatecalendardayofweek

提问by kaibuki

I want to get the starting and ending dates of a week for example

例如,我想获取一周的开始和结束日期

2012-05-06 to 2012-05-12
2012-05-13 to 2012-05-19

2012-05-06 至 2012-05-12
2012-05-13 至 2012-05-19

The code I have written is

我写的代码是

currWeekCalender.add(Calendar.WEEK_OF_YEAR, 1);

    String dateStart =  currWeekCalender.get(Calendar.YEAR) + "-" + addZero((currWeekCalender.get(Calendar.MONTH) + 1)) + "-" + addZero(currWeekCalender.getFirstDayOfWeek());
    currWeekCalender.add(Calendar.DAY_OF_MONTH,7);
    String dateEnd =  currWeekCalender.get(Calendar.YEAR) + "-" + addZero((currWeekCalender.get(Calendar.MONTH) + 1)) + "-" + addZero(currWeekCalender.get(Calendar.DAY_OF_MONTH));

but the results are not correct, also I want previous weeks date.

但结果不正确,我也想要前几周的日期。

Thanks

谢谢

采纳答案by Mike Samuel

Your problem is that getFirstDayOfWeek()returns the first day of the week; e.g., Sunday in US, Monday in France. It does not return a day of the month. See javadoc.

您的问题是getFirstDayOfWeek()返回一周的第一天;例如,美国的星期天,法国的星期一。它不会返回一个月中的某一天。请参阅javadoc

The first day in a month that is the start of the week is (in pseudo-code)

一个月中的第一天是一周的开始(在伪代码中)

((7 + (firstDayOfWeek - dayOfWeek(firstOfMonth))) % 7) + 1

You can translate that into java.util.Calendarcode if you like, but I would suggest using Joda time instead.

java.util.Calendar如果您愿意,您可以将其翻译成代码,但我建议您改用 Joda 时间。



also I want previous weeks date.

我也想要前几周的日期。

Just subtract seven days maybe using add

只需减去 7 天,也许使用 add

currCalendar.add(Calendar.DAY_OF_MONTH, -7)

This may involve underflow, but adddeals with that.

这可能涉及下溢,但要add处理它。

add(f, delta)

adds delta to field f. This is equivalent to calling set(f, get(f) + delta)with two adjustments:

Add rule 1.The value of field f after the call minus the value of field f before the call is delta, modulo any overflow that has occurred in field f. Overflow occurs when a field value exceeds its range and, as a result, the next larger field is incremented or decremented and the field value is adjusted back into its range.

add(f, delta)

将 delta 添加到字段 f。这相当于调用set(f, get(f) + delta)有两个调整:

添加规则 1.调用后字段 f 的值减去调用前字段 f 的值是 delta,以字段 f 中发生的任何溢出为模。当字段值超出其范围时会发生溢出,因此,下一个较大的字段会增加或减少,并且字段值会调整回其范围。

回答by ogniwo100

Hello to all coders :)

向所有编码员问好:)

I work on little app to dive some data from database. To calculate previous weeks start and end date i use this code:

我在小应用程序上工作以从数据库中挖掘一些数据。要计算前几周的开始和结束日期,我使用以下代码:

// Calendar object
Calendar cal = Calendar.getInstance();

// "move" cal to monday this week (i understand it this way)
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

// calculate monday week ago (moves cal 7 days back)
cal.add(Calendar.DATE, -7);
Date firstDateOfPreviousWeek = cal.getTime();

// calculate sunday last week (moves cal 6 days fwd)
cal.add(Calendar.DATE, 6);
Date lastDateOfPreviousWeek = cal.getTime();

Hope, that helps.

希望,这有帮助。

回答by freedev

Java 8 version

Java 8 版本

This prints previous 10 weeks

这将打印前 10 周

final ZonedDateTime input = ZonedDateTime.now();

for(int i = 1; i < 10; i++) {
    final ZonedDateTime startOfLastWeek = input.minusWeeks(i).with(DayOfWeek.MONDAY);
    System.out.print(startOfLastWeek.format(DateTimeFormatter.ISO_LOCAL_DATE));
    final ZonedDateTime endOfLastWeek = startOfLastWeek.plusDays(6);
    System.out.println(" - " + endOfLastWeek.format(DateTimeFormatter.ISO_LOCAL_DATE));
}