Skip to content

Instantly share code, notes, and snippets.

@jonelf
Last active September 15, 2015 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonelf/c79bd3ed62d09d3f0c20 to your computer and use it in GitHub Desktop.
Save jonelf/c79bd3ed62d09d3f0c20 to your computer and use it in GitHub Desktop.
Week numbers are hard.
// Week numbers are hard. Here's a bug I found earlier today.
// If you want to construct a string like this
String yearWeekKey = year + week;
// then you can't use
String year = new SimpleDateFormat("yyyy").format(date);
String week = String.format("%02d", new LocalDate(date).getWeekOfWeekyear());
// when date is 2016-01-01 because the key will be "201653" and that week does not exist.
// Instead you have to use .weekyear() because that correctly returns 2015 for 2016-01-01.
String year = new DateTime(ets).weekyear().getAsString();
// Result "201553"
// PS. Joda-Time and ISO 8601 applies DS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment