Yogi
Yogi

Reputation: 1047

Get range of Week of Year from 0

I am using following to get week of year .

SimpleDateFormat dateFormatforYearWeek = new SimpleDateFormat("yyyyww");
String s = dateFormatforYearWeek.format(date)

For Oct 26 this gives me value as 201444. However I want it to get as 201443. I am not sure how to set week of year as 0. Is it possible? If not how can i modify it.

Is this the correct way?

int day = calendar.get(Calendar.DAY_OF_MONTH);
int year = calendar.get(Calendar.YEAR);
calendar.add(Calendar.WEEK_OF_YEAR, -1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.YEAR, year);

SimpleDateFormat dateFormatforYearWeek = new SimpleDateFormat("yyyyww");
String s = dateFormatforYearWeek.format(date)

Upvotes: 0

Views: 955

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 340070

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 ) ;

Upvotes: 0

I would probably do it the following way then:

SimpleDateFormat dateFormatForYear = new SimpleDateFormat("yyyy");
SimpleDateFormat yearWeekFormat    = new SimpleDateFormat("w");
Integer          weekFrom0         = Integer.valueOf(yearWeekFormat.format(date)) - 1;
String           s                 = dateFormatForYear.format(date) + weekFrom0; 

Upvotes: 0

Semih Eker
Semih Eker

Reputation: 2409

this post can help you:

Why dec 31 2010 returns 1 as week of year?

Try sthg like this:

       Calendar calDe = Calendar.getInstance(Locale.GERMAN);       

Upvotes: 1

Related Questions