Cycle increment value when repeating after weeks of the week

In my android app, I use the following Map

to map class days Calendar

to String

s:

Map<Integer, String> map = new HashMap<>(7);
                                             // values of the Calendar field
map.put(Calendar.MONDAY, "Mon");             // 2
map.put(Calendar.TUESDAY, "Tue");            // 3
map.put(Calendar.WEDNESDAY, "Wed");          // 4
map.put(Calendar.THURSDAY, "Thu");           // 5
map.put(Calendar.FRIDAY, "Fri");             // 6
map.put(Calendar.SATURDAY, "Sat");           // 7
map.put(Calendar.SUNDAY, "Sun");             // 1

      

And adding Button

dynamically to LinearLayout

with the text of the day like this:

LinearLayout dayLayout = (LinearLayout) findViewById(R.id.layout_days);

for(int i = 1; i <= 7; i++) {
    Button button = new Button(this);
    button.setText(map.get(i));
    dayLayout.addView(button);
}

      

This works great, but the list Button

always starts on Sunday, regardless of the current one Locale

.

I am trying to rewrite this loop to take into account the first day of the week.

Something like that:

LinearLayout dayLayout = (LinearLayout) findViewById(R.id.layout_days);
int firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();

for(int i = firstDayOfWeek; i <= 7; ???) { // this is where i'm stuck
    Button button = new Button(this);
    button.setText(map.get(i));
    dayLayout.addView(button);
}

      

The problem is that I cannot figure out the correct formula to get the days in the correct order.

If the first day of the week is Monday, the cycle should go like 2 3 4 5 6 7 1

.

I guess I need to play with the modulo operator, I just don't know how.

I would really appreciate any advice, maybe my approach is completely wrong.

+3


source to share


6 answers


First of all, you shouldn't use numeric constant values Calendar.*

. Numeric values ​​are an implementation detail, not an API, and cannot be used directly like for-loop attempts.

Better to keep the constants in a list:

List<Integer> days = Arrays.asList(
    Calendar.MONDAY,
    Calendar.TUESDAY,
    Calendar.WEDNESDAY,
    Calendar.TUESDAY,
    Calendar.FRIDAY,
    Calendar.SATURDAY,
    Calendar.SUNDAY
    );

      

And then find the index whose value matches firstDayOfWeek

:



int start = days.indexOf(firstDayOfWeek);

      

And then create the correct map key by rotating the 0..6 indices by value start

and the modulo operator, for example:

for(int i = 0; i < 7; i++) {
  int key = days.get((start + i) % 7);
  Button button = new Button(this);
  button.setText(map.get(key));
  dayLayout.addView(button);
}

      

+2


source


Starting with i

how 1 2 3 4 5 6 7

,
you can take this mod 7 by giving 1 2 3 4 5 6 0


and then add 1: giving 2 3 4 5 6 7 1

.



0


source


it is possible to add count to a for loop and compare count to 7 so that the loop will loop through all 7 days regardless of the start.

so something like this

LinearLayout dayLayout = (LinearLayout) findViewById(R.id.layout_days);
int firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
int count = 1;

for(int i = firstDayOfWeek; count <= 7; i++) {

count++    
if(i <= 7)
    firstDayOfWeek = 1;

Button button = new Button(this);
button.setText(map.get(i));
dayLayout.addView(button);
}

      

0


source


Define an array like this:

int[] daysInWeek = [2, 3, 4, 5, 6, 7, 1]

      

Then the solution should be:

// define day of week you want to print out first.
int firstDayOfWeekIndex = 0;  // print Monday first
for (int i = 0; i < 7; i++) {
  int currentDay = (firstDayOfWeekIndex + i) % 7;
  String day = map.get(daysInWeek[currentDay]);

  // print this day to your button
  Button button = new Button(this);
  button.setText(day);
  dayLayout.addView(button);
}

      

0


source


Isn't that what you want?

Map<String, Integer> names = (new GregorianCalendar()).getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.ENGLISH);

      

which gives

Tue, 3
Thu, 5
Sun, 1
Sat, 7
Wed, 4
Mon, 2
Fri, 6

      

0


source


Here is a complete example that also removes the need for a Map to display weekdays in the correct locale.

DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
// Array with 8 elements (first is empty string), so Calendar constants correspond to the day.
String[] dayNames = symbols.getShortWeekdays();

int firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
int daysStart = Calendar.SUNDAY;
int daysEnd = Calendar.SATURDAY;
int dayCount = (daysEnd - daysStart) + 1; // +1 because both ends inclusive

for(int i = 0; i < dayCount; i++) {

    int day = firstDayOfWeek + i;
    if(day > daysEnd) // loop back if over the end
        day -= dayCount;

    Button button = new Button(this);
    button.setText(dayNames[day]);
    dayLayout.addView(button);
}

      

The day starts with firstDayOfWeek: 2 3 4 5 6 7 1


Russian: Mon Tue Wed Thu Fri Sat Sun


Japanese:月 火 ζ°΄ 木 金 土 ζ—₯

0


source







All Articles