3 ms·
1: Should have used a library function 2: not 0-indexed (there can be good reasons for this, but the context of the function should at least suggest the reason
by mseebach 13y ago
1: Should have used a library function
2: not 0-indexed (there can be good reasons for this, but the context of the function should at least suggest the reason)
3: Doesn't handle errors - should throw IllegalArgumentException, and if should do it in the default handler of the switch. If you suddenly see "???" in the UI, it's not easy to track down to this.
4: If for some valid reason this can't be a library function, it should be an enum, and it should be used throughout the code.
enum DayOfWeek {
MONDAY(1, "Monday"), TUESDAY(2, "Tuesday") ...
public int id;
public String displayName; // these should be private and have getters according to most style-guides.
public DayOfWeek(int id, String displayName) {
this.id = id;
this.displayName = displayName;
}
public static DayOfWeek fromId(int id) {
for (DayOfWeek d : values()) {
if (d.id == id) return d;
}
throw new IllegalArgumentException("Illegal DayOfWeek id passed: " + id);
}
}
- ygra 13y agoFWIW, the default Java API to get weekday strings is also 1-based (DateFormatSymbols.getWeekdays(); and Calendar.SUNDAY is 1).
- anton_gogolev 13y agoYeah, with Java DateTime API being royally fubarred [1]. [1]: http://stackoverflow.com/q/1969442/60188 http://stackoverflow.com/q/1969442/60188
- melignus 13y ago... every DateTime API ... FTFY
- a3voices 13y agoIn iOS, days aren't 0 indexed either by default. Sunday starts at one.