简体   繁体   中英

Get start and end of week on Android

I wonder how could I calculate start and end days of the current week? I've found that this it not implemented in standard android libs or such lib as date4j.

If there some easy and plain way to implement this? Or I have to implement bicycle again?

Thanks.

It doesn't take much code to do this with date4j. An example of calculating the first day of the week:

 private void firstDayOfThisWeek(){
    DateTime today = DateTime.today(TimeZone.getDefault()); 
    DateTime firstDayThisWeek = today; //start value 
    int todaysWeekday = today.getWeekDay();
    int SUNDAY = 1;
    if(todaysWeekday > SUNDAY){
      int numDaysFromSunday = todaysWeekday - SUNDAY;
      firstDayThisWeek = today.minusDays(numDaysFromSunday);
    }
    System.out.println("The first day of this week is : " + firstDayThisWeek);
  }

The above follows the convention that Sunday is the start of the week. In some jurisdictions, that convention doesn't apply, so you would need to change the code for such cases.

Maybe MonthDisplayHelper could be of help for you.

Good luck!

This worked for me...

Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
cal.set(Calendar.DAY_OF_WEEK, cal.MONDAY);
String firstWkDay = String.valueOf(cal.getTime());
//cal.set(Calendar.DAY_OF_WEEK, cal.SUNDAY);
cal.add(Calendar.DAY_OF_WEEK, 6);
String lastWkDay =  String.valueOf(cal.getTime());

For last day of the week, you could use cal.set(Calendar.DAY_OF_WEEK, cal.SUNDAY); but it returns previous sunday on some devices

This kind of date-time work is often easier using the Joda-Time 2.3 library.

Taken from this answer to a similar question.

LocalDate now = new LocalDate();
System.out.println( now.withDayOfWeek(DateTimeConstants.MONDAY) );
System.out.println( now.withDayOfWeek(DateTimeConstants.SUNDAY) ); 

You can call isBefore / isAfter and minusWeeks / plusWeeks to get past/future values.

This is what I have been using to get the date:

 Calendar c = Calendar.getInstance();
 c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
 c.setFirstDayOfWeek(Calendar.MONDAY);

 startOfWeek = c.getTime();

Try java.util.Calendar.getFirstDayOfWeek()... then it's easy to calculate the last day of week: http://developer.android.com/reference/java/util/Calendar.html#getFirstDayOfWeek ()

I have written my own logic for this.

fun getStartDateAndEndDateOfWeek(myDate :String) : ArrayList<String>{
        var dates:ArrayList<String> = arrayListOf()
        var cal = Calendar.getInstance()
        cal.time = SimpleDateFormat("yyy/MM/dd").parse(myDate)
        Log.e("==== ","== My Date  : ${SimpleDateFormat("EEE,dd MMM yyyy").format(SimpleDateFormat("yyy/MM/dd").parse(myDate))} ")
        Log.e("== ","Day of Week : ${cal.get(Calendar.DAY_OF_WEEK)}")

        var startDate =""
        var endDate =""

        when(cal.get(Calendar.DAY_OF_WEEK)){
            1 ->{
                var date = getOldDate(6,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(date)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(cal.time)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Sunday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            2 ->{
                var sdate = getOldDate(0,cal.time)
                var edate = getOldDate(-6,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Monday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            3 ->{
                var sdate = getOldDate(1,cal.time)
                var edate = getOldDate(-5,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Tuesday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            4 ->{
                var sdate = getOldDate(2,cal.time)
                var edate = getOldDate(-4,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Wednesday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            5 ->{
                var sdate = getOldDate(3,cal.time)
                var edate = getOldDate(-3,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Thursday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            6 ->{
                var sdate = getOldDate(4,cal.time)
                var edate = getOldDate(-2,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"
                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Friday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
            7 ->{
                var sdate = getOldDate(5,cal.time)
                var edate = getOldDate(-1,cal.time)
                startDate = "${SimpleDateFormat("EEE,dd MMM yyyy").format(sdate)}"
                endDate ="${SimpleDateFormat("EEE,dd MMM yyyy").format(edate)}"

                Log.e("======","----------------------------------------------------")
                Log.e("======","----------------User Date is Saturday------------------")
                Log.e("======","----------------------------------------------------")

                Log.e("======","Start Date : $startDate")
                Log.e("======","End Date : $endDate")

                Log.e("======","----------------------------------------------------")
                dates.add(startDate)
                dates.add(endDate)
            }
        }

        return dates
        //getStartEndOFWeek(cal.get(Calendar.WEEK_OF_YEAR),cal.get(Calendar.YEAR),myDate)
    }


    fun getOldDate(daysAgo: Int,myDate:Date): Date {
        val calendar = Calendar.getInstance()
        /*calendar.set(Calendar.MONTH, APRIL)
        calendar.set(Calendar.DAY_OF_MONTH,6)
        calendar.set(Calendar.YEAR,2020)
        Log.e("==== ","=====  Date  : ${calendar.time}")*/
        calendar.add(Calendar.DAY_OF_YEAR, -daysAgo)
        //Log.e("==== ","=====  Date $daysAgo ago : ${calendar.time}")
        return calendar.time
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM