0
votes

For some weird reason.. Calendar.getInstance().get(Calendar.WEEK_OF_MONTH) returns 0, while command in linux returns 1

root@ns4009576:~# echo $((($(date +%d)-1)/7+1))

1

In the beginning i thought it could be that first day is Monday, so Sunday (today) is month 0 (?) but that doesn't seem to be the case either, locale returns Sunday as first day

root@ns4009576:~# locale day

Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday

Any help would be appreciated, thank you.

1
Why are you comparing the result of a java method and a linux command ? - Julien
You better print out some details about your calendar like System.out.println(Calendar.getInstance()); - Michael Konietzka

1 Answers

0
votes

Java may not respect the same locale rules as your date command. Comparing the two is probably not a great idea.

You are getting 0 as the week of the month because Sunday is not defined as your first day of the week. Try adding this to your code to see the difference:

calendar.setFirstDayOfWeek(Calendar.SUNDAY);