What Is A Week?
The doc for SimpleDateFormat
fails to define what they mean by week. Other sources suggest they intended the standard ISO 8601 definition of week. But then they violate that by defining calendar.getMinimalDaysInFirstWeek() == 1
rather than 4, as discussed in this other Answer.
What do you mean by a week of year in your Question?
ISO 8601
As mentioned above, the ISO 8601 defines a week of year as beginning on Monday, and where the first week contains the first Thursday of the year.
This standard also defines a string format to represent a week of year: YYYY-Www
or YYYYWww
. Note the W
in the middle. That letter is important as it avoids ambiguity with the year-month format YYYY-MM
. I suggest you conform to ISO 8601 if at all possible.
Zero-Based Week Counting
I've never heard of counting weeks from zero. That makes no sense to me; I suggest avoiding this if at all possible.
If not possible, I suggest you use a date-time library to calculate standard week of year and subtract one. But I'm sure this is a bad road to travel.
Time Zone
Time zone is crucial in determining a date and therefore a week. At the stroke of midnight ending Sunday in Paris means a new week in France while still "last week" in Montréal.
Joda-Time
The old date-tim classes in Java are notoriously troublesome, confusing, and flawed: java.util.Date, java.util.Calendar, java.text.SimpleDateFormat. Avoid them.
Instead use either Joda-Time or the java.time package built into Java 8 (inspired by Joda-Time).
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) );
String output = ISODateTimeFormat.weekyearWeek().print( now );
When run.
now: 2014-11-03T02:30:10.124-05:00
output: 2014-W45
If you must insist on that zero-based week number:
int zeroBasedWeekNumber = ( now.getWeekOfWeekyear() - 1 ) ;