0

I am trying to get week number for a given date. But its working for few dates and not working for couple of dates.

Followed this link https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

 select from_unixtime(unix_timestamp('2015-12-31','yyyy-MM-dd'),'w') week_in_year;

The above code is returning 1

 select from_unixtime(unix_timestamp('2016-12-31','yyyy-MM-dd'),'w') week_in_year;

The above code is returning 53. I think the first code snippet also should give something like 53/52. Any help is appreciated in solving.

sandeep007
  • 348
  • 3
  • 16
  • Both are plausibly correct. ISO 8601 lays down the rules. You can find a lot of information in my answer to [How do I calculate the week number given a date?](https://stackoverflow.com/questions/274861/how-do-i-calculate-the-week-number-given-a-date/275024?s=2|58.0167#275024) – Jonathan Leffler Dec 28 '17 at 05:48
  • If your question is: "Which week does 2015-12-31 fall in?" then my answer, based on 'day 1 is Monday', is 2015-12-31 = 2015-W53 = 2015-W53-4. For 2016-12-31, my answer is 2016-12-31 = 2016-W52 = 2016-W52-6. I would have to work harder to make the code work with '1st day of week is Sunday'. – Jonathan Leffler Dec 28 '17 at 06:10
  • I observe that 2015-12-31 was Thursday (day 4); 2016-12-31 was Saturday (day 6) — the Unix `cal` program confirms those observations. – Jonathan Leffler Dec 28 '17 at 06:20

0 Answers0