Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...