diff --git a/app/src/main/java/org/isoron/uhabits/helpers/DateHelper.java b/app/src/main/java/org/isoron/uhabits/helpers/DateHelper.java index b2c300453..81ad27b9a 100644 --- a/app/src/main/java/org/isoron/uhabits/helpers/DateHelper.java +++ b/app/src/main/java/org/isoron/uhabits/helpers/DateHelper.java @@ -25,6 +25,7 @@ import android.text.format.DateFormat; import org.isoron.uhabits.R; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; @@ -155,17 +156,50 @@ public class DateHelper } + /** + * Throughout the code, it is assumed that the weekdays are numbered + * from 0 (Saturday) to 6 (Friday). + * + * see https://github.com/iSoron/uhabits/issues/74 + * + * In the Java Calendar they are numbered from 1 (Sunday) to 7 (Saturday) + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
daydayNumberwdaysIndex
Su11
Mo22
Tu33
We44
Th55
Fr66
Sa70
+ * + * So we have {@code wdaysIndex = dayNumber % 7} + * + * @return array with names from Saturday to Friday according to the current locale + * + * @see #getWeekday(long) + * @see java.util.Calendar#SUNDAY + * + */ public static String[] getDayNames(int format) { String[] wdays = new String[7]; - GregorianCalendar day = new GregorianCalendar(); - day.set(GregorianCalendar.DAY_OF_WEEK, 0); + Calendar day = new GregorianCalendar(); + // we start with Saturday + day.set(GregorianCalendar.DAY_OF_WEEK, Calendar.SATURDAY); - for (int i = 0; i < 7; i++) + for (int i = 0; i < wdays.length; i++) { wdays[i] = day.getDisplayName(GregorianCalendar.DAY_OF_WEEK, format, Locale.getDefault()); + // advance in time by one day day.add(GregorianCalendar.DAY_OF_MONTH, 1); }