Eclipse Android Java Calendar Start Week On Monday Or Sunday

When using the Calendar class in Android Java the default start of the week is Sunday. To change this use the following function setFirstDayOfWeek

Calendar cal;
cal.setCalendar(Calendar.getInstance());
cal.setFirstDayOfWeek(Calendar.MONDAY);

cal.getInstance() set it to the current date.

setFirstDayOfWeek takes an integer. Calendar.MONDAY is just an integer value of 1. Sunday has the value 0, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5 and Saturday = 6

To get the week number from Calendar you can do this:

String weeknr = String.format("%d", cal.get(Calendar.WEEK_OF_YEAR);
Posted in Android, Tips and Tricks Tagged with: , , ,