5

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.

Igor Konoplyanko
  • 9,176
  • 6
  • 57
  • 100
  • possible duplicate of [Getting the dates of the current week in Android](http://stackoverflow.com/questions/10451784/getting-the-dates-of-the-current-week-in-android) – Basil Bourque Jun 17 '14 at 14:01

7 Answers7

7

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.

John
  • 1,635
  • 15
  • 22
5

Maybe MonthDisplayHelper could be of help for you.

Good luck!

  • 1
    Why this answer is accepted one? This class is not returning first day of the week correctly. It should return SUNDAY and it relies on Calendar. It has two initialization constructors and all of them are useless: public MonthDisplayHelper(int year, int month) { this(year, month, Calendar.SUNDAY); } and another one public MonthDisplayHelper(int year, int month, int weekStartDay) {} which will end up with Sunday and ANY week day as a first week day. How about USA or Ukraine with Monday as first week day? This makes no sence – Roman T. Aug 29 '19 at 18:33
2

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

aphoe
  • 2,586
  • 1
  • 27
  • 31
  • "but it returns previous sunday on some devices" can you explain why? because i think my device having this problem. – Amir Fazwan Dec 08 '15 at 06:48
  • The short answer is... Android fragmentation. Try the code snippet as I wrote above – aphoe Dec 08 '15 at 14:30
1

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

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();
Neorcisa
  • 124
  • 2
  • 9
1

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

home
  • 12,468
  • 5
  • 46
  • 54
0

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
    }