How to get the last Sunday before current date?

2020-06-08 03:24发布

I have the following code for getting the last Sunday before the current date:

Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR)-1);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Log.e("first day", String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));

But this code doesn't work. Please, tell me, how can I fix it?

7条回答
Juvenile、少年°
2楼-- · 2020-06-08 04:00

java.time.temporal.TemporalAdjuster

This can be easily achieved by a TemporalAdjuster implementation found in TemporalAdjusters along with the DayOfWeek enum. Specifically, previous​(DayOfWeek dayOfWeek).

 LocalDate
 .now()
 .with(
     TemporalAdjusters.previous( DayOfWeek.SUNDAY )
 ) ;

If today is Sunday and you would like the result to be today rather than a week ago, call previousOrSame​(DayOfWeek dayOfWeek).

 LocalDate
 .now()
 .with(
     TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY )
 ) ;

These classes are built into Java 8 and later, and built into Android 26 and later. For Java 6 & 7, most of the functionality is back-ported in ThreeTen-Backport. For earlier Android, see ThreeTenABP.

查看更多
我想做一个坏孩纸
3楼-- · 2020-06-08 04:01

Here's how to do it with a Kotlin extension and the Joda Time library.
Answer is based on linqu's answer but this also fixes a bug in that answer. The bug was that it did not work if the current date was the same day you're trying to get.

/**
 * E.g., DateTimeConstants.SUNDAY
 */
fun DateTime.previousDayOfWeek(day: Int): DateTime {
    var date = this
    if(date.dayOfWeek == day) date = date.minusDays(1)

    val offset = (date.dayOfWeek - day + 7) % 7
    return date.minusDays(offset)
}

fun DateTime.previousSunday(): DateTime {
    return previousDayOfWeek(DateTimeConstants.SUNDAY)
}
查看更多
Ridiculous、
4楼-- · 2020-06-08 04:02
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis() //
     // Saturday is the 7th day of week, so use modulo to get it : remove day between todoay
     - (( cal.get(Calendar.DAY_OF_WEEK) % 7) * 86400000)); // 86400000=24*60*60*1000

System.out.println(cal.getTime());
. . .
查看更多
太酷不给撩
5楼-- · 2020-06-08 04:03

The following works for me irrespective of which month and year.

 Calendar cal = Calendar.getInstance(TimeZone.getDefault());
 Date date = cal.getTime();
 int days = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
 date.setTime(date.getTime() - (long) (days*1000*60*60*24));
 cal.setTime(date);
查看更多
够拽才男人
6楼-- · 2020-06-08 04:10

Here is a snippet to calculate any last day of the week with Joda:

import org.joda.time.DateTime
import org.joda.time.DateTimeConstants

DateTime now = DateTime();
int offset = ((now.dayOfWeek - DateTimeConstants.THURSDAY) + 7) % 7;
DateTime lastThursday = now.minusDays(offset);

Just replace DateTimeConstants.THURSDAY with your day of choice.

查看更多
We Are One
7楼-- · 2020-06-08 04:17

This will work. We first get the day count, and then subtract that with the current day and add 1 ( for sunday)

Calendar cal=Calendar.getInstance();
cal.add( Calendar.DAY_OF_WEEK, -(cal.get(Calendar.DAY_OF_WEEK)-1)); 
System.out.println(cal.get(Calendar.DATE));

Edit : As pointed out by Basil Bourque in the comment, see the answer by Grzegorz Gajos for Java 8 and later.

查看更多
登录 后发表回答