Skip to content

Instantly share code, notes, and snippets.

@mattnworb
Created May 4, 2011 13:41
Show Gist options
  • Save mattnworb/955224 to your computer and use it in GitHub Desktop.
Save mattnworb/955224 to your computer and use it in GitHub Desktop.
/**
* Code sample for http://stackoverflow.com/questions/5877226/better-way-to-check-if-a-joda-time-interval-spans-exactly-1-calendar-week-accoun
* @author: matt b
* @since: 5/4/11
*/
public class JodaTimeWeekPeriodTest {
public static void main(String[] args) {
DateTime start = new DateTime(2011, 5, 4, 0, 0, 0, 0);
DateTime end = new DateTime(2011, 5, 11, 0, 0, 0, 0);
testEquality(new Interval(start, end));
//DST in US in 2011 started on March 13
DateTime start2 = new DateTime(2011, 3, 12, 0, 0, 0, 0);
DateTime end2 = new DateTime(2011, 3, 19, 0, 0, 0, 0);
testEquality(new Interval(start2, end2));
}
private static void testEquality(Interval interval) {
Period period = interval.toPeriod();
System.out.printf("Interval %s equal to Weeks.ONE? %s\n", interval, period.equals(Weeks.ONE));
System.out.printf("Interval %s equal to Period.weeks(1)? %s\n", interval,
period.equals(Period.weeks(1)));
System.out.printf("Interval %s plus one hour equal to Period.weeks(1)? %s\n", interval,
period.plusHours(1).equals(Period.weeks(1)));
}
}
/* Output:
*
* Interval 2011-05-04T00:00:00.000/2011-05-11T00:00:00.000 equal to Weeks.ONE? false
* Interval 2011-05-04T00:00:00.000/2011-05-11T00:00:00.000 equal to Period.weeks(1)? true
* Interval 2011-05-04T00:00:00.000/2011-05-11T00:00:00.000 plus one hour equal to Period.weeks(1)? false
*
* Interval 2011-03-12T00:00:00.000/2011-03-19T00:00:00.000 equal to Weeks.ONE? false
* Interval 2011-03-12T00:00:00.000/2011-03-19T00:00:00.000 equal to Period.weeks(1)? true
* Interval 2011-03-12T00:00:00.000/2011-03-19T00:00:00.000 plus one hour equal to Period.weeks(1)? false
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment