Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get start and end of week on Android

Tags:

java

date

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.

like image 441
Igor Konoplyanko Avatar asked Jul 07 '11 22:07

Igor Konoplyanko


2 Answers

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.

like image 165
John Avatar answered Sep 23 '22 02:09

John


Maybe MonthDisplayHelper could be of help for you.

Good luck!

like image 25
Carlos Martinez Avatar answered Sep 27 '22 02:09

Carlos Martinez