robert trudel
robert trudel

Reputation: 5749

Compare date with jodatime

I try to compare some date with jodaTime, i use week and year to compare date

DateMidnight endDate01 = new DateMidnight().withYear(2012).withWeekyear(37);
DateMidnight endDate02 = new DateMidnight().withYear(2012).withWeekyear(38);


endDate01.isBefore(endDate02); //return false

I don't understand why endDate01 seem greater the endDate02.

Maybe a bug?

DateMidnight endDate01 = new DateMidnight();

endDate01.withYearOfEra(o1.getEndYearPeriod());
endDate01.withWeekOfWeekyear(o1.getEndWeekPeriod());

System.out.println(o1.getEndWeekPeriod() + " " + o1.getEndYearPeriod());
System.out.println(endDate01.getWeekOfWeekyear() + " " + endDate01.getYearOfEra());

i get

37 2012

16 2012

should get the same result, no?

Upvotes: 0

Views: 1511

Answers (3)

Zhivko Draganov
Zhivko Draganov

Reputation: 1253

DateTime firstDate = new DateTime();
firstDate.withWeekyear(2012) /* represents the 2012 with number of weeks in it (I thing the algorith is the difference between first week in 1970) */
firstDate.withWeekOfWeekyear(29) /* now you have the 29th week of 2012 */
firstDate.withDayOfWeek(1) /* now you have 29th week of 2012 and Monday */

Now you set the date! Set the second this way and compare them.

Upvotes: 0

robert trudel
robert trudel

Reputation: 5749

those both code work fine

DateTime dt = new DateTime(); 
dt = dt.withWeekOfWeekyear(29); 
dt = dt.withWeekyear(2011);

DateMidnight dt2 = new DateMidnight().withWeekOfWeekyear(29).withYear(2011);

Upvotes: 1

Louis Wasserman
Louis Wasserman

Reputation: 198023

withWeekyear isn't the same as withWeekOfWeekyear. I'm not 100% sure what withWeekyear does instead, I think if you mean the 37th week of 2012, you should use withWeekOfWeekyear instead.

Upvotes: 0

Related Questions