Here is the SimpleDateFormat documentation for reference.

The week in year format does not seem to function correctly at year end. For example, if I try to format 2014-12-30 to get this information, I get a value of 1. I would expect 52.

Datetime example = Datetime.newInstanceGMT(
    Date.newInstance(2014, 12, 30),
    Time.newInstance(12, 0, 0, 0)
system.debug(example.format('w')); // "Week in year" - outputs 1
system.debug(example.format('W')); // "Week in month" - outputs 5

Since week in month should only ever be greater than week in year in this peculiar circumstance, it seems I can implement the following workaround:

public Integer getWeekInYear(Datetime toParse)
    Integer weekInYear = Integer.valueOf(toParse.format('w'));
    Integer weekInMonth = Integer.valueOf(toParse.format('W'));
    return (weekInMonth > weekInYear) ? 52 : weekInYear;

Is this a bug in either the DateTime type or SimpleDateFormat? Is the logic in getWeekInYear sound?

  • Practically speaking based on the calender dates, 2014-12-21 to 2014-12-27 is the 52nd week. 2014-12-30 and 2015-01-01 fall under the same week which is first week of the year. Please let me know if you think I'm wrong. :)
    – cartman
    Commented Sep 11, 2015 at 6:26
  • I do, in that case I would think 2014-12-30 falls under the 53rd week of 2014.
    – Adrian Larson
    Commented Sep 11, 2015 at 6:38

1 Answer 1


It is nothing to do with Salesforce.

As per ISO 8601 If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year (there is no week 00). 28 December is always in the last week of its year. The ISO week-numbering year starts at the first day (Monday) of week 01 and ends at the Sunday before the new ISO year (hence without overlap or gap). It consists of 52 or 53 full weeks.

According this 1st week of 2015 is Dec 28, 2014- Jan 3, 2015. That's why you are getting week no as 1.

You can validate the week no here. http://www.timeanddate.com/date/weeknumber.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .