What is the best way to represent week of year with Joda-Time library? I'm looking something as elegant as YearMonth
is for representing month of year.

- 303,325
- 100
- 852
- 1,154

- 12,170
- 10
- 58
- 70
-
1Do you mean a week of a *specific* year? – Jon Skeet Jul 31 '12 at 13:03
-
@Jon No I need week across more years. – michal.kreuzman Jul 31 '12 at 13:08
-
Then isn't that just an integer? – Jon Skeet Jul 31 '12 at 13:09
-
With just an integer I'm loosing information about particular year...For example I need to distinguish between 30.week 2011 and 30.week 2012 – michal.kreuzman Jul 31 '12 at 13:12
-
1So that means you *do* want to be specific about the year after all... you don't want "week across more years" - you want each value to be a specific week in a specific year. – Jon Skeet Jul 31 '12 at 13:21
-
Yes sorry I misunderstood it. Yes I want to be specific about year. – michal.kreuzman Jul 31 '12 at 13:28
2 Answers
After some searching I found solution using Partial
class for this problem.
Partial yearWeek = new Partial(
new DateTimeFieldType[] {DateTimeFieldType.weekyear(),
DateTimeFieldType.weekOfWeekyear()},
new int[] {year, week});

- 12,170
- 10
- 58
- 70
-
2If you are using this extensively you may want to consider implementing the `YearWeek` partial as explicit final subclass of `BasePartial`, using joda's implementation of `YearMonth` as blueprint. And while on it, you could even contribute it back. :-) – benjamin Nov 27 '12 at 17:48
-
@benjamin I exactly did this, because I need it at more places in application. So I can contribute it. But is it appropriate place it here? – michal.kreuzman Nov 28 '12 at 14:02
-
I have neither contributed to JodaTime yet, but I don't think SOF is the right place to put it. The easiest way is probably to file a feature request on [jodatime's issue tracker](http://sourceforge.net/p/joda-time/bugs/) and attach your class and test sources. Then the jodatime team may descide to accept it or not to, but anyone looking for that feature may find your code and use it. – benjamin Nov 28 '12 at 15:08
-
2@michal.kreuzman hi michal, i need a similar thing, did you share your code anywhere? – Marsellus Wallace Jan 09 '14 at 17:26
tl;dr
org.threeten.extra.YearWeek.of( 2018 , 28 )
java.time
The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
org.threeten.extra.YearWeek
The java.time classes are extended by the ThreeTen-Extra project. This project includes the YearWeek
class, just what you need.
Beware that "week" can have many definitions. The one used here is defined by the ISO 8601 standard, where the week # 1 contains the first Thursday of the calendar year, and Monday starts each week. So a year has either 52 or 53 weeks. A week-based year may contain a few days from the previous/succeeding calendar year around the end of December and beginning of January.
Pass the number of the week-based year, and the week number.
YearWeek yw = YearWeek.of( 2018 , 7 ) ;
Get a date within that week by specifying a day-of-week via the DayOfWeek
enum.
LocalDate wednesdayOf2018W07 = yw.atDay( DayOfWeek.THURSDAY ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154