patternjavaMinor
Splitting a date-range - can it be improved somehow?
Viewed 0 times
cansomehowimprovedrangesplittingdate
Problem
I've created a method to split a date range into a collection of date-arrays. These arrays consist of two timestamps.
Example:
Results into:
```
*****
Split 2.1.2004 to 26.3.2004:
2004-01-02T00:00:00.000+01:00 and 2004-01-31T00:00:00.000+01:00
2004-02-01T00:00:00.000+01:00 and 2004-02-29T00:00:00.000+01:00
2004-03-01T00:00:00.000+01:00 and 2004-03-26T00:00:00.000+01:00
*****
Split 1.1.2000 to 31.12.2005:
2000-01-01T00:00:00.000+01:00 and 2000-01-31T00:00:00.000+01:00
2000-02-01T00:00:00.000+01:00 and 2000-02-29T00:00:00.000+01:00
2000-03-01T00:00:00.000+01:00 and 2000-03-31T00:00:00.000+02:00
2000-04-01T00:00:00.000+02:00 and 2000-04-30T00:00:00.000+02:00
2000-05-01T00:00:00.000+02:00 and 2000-05-31T00:00:00.000+02:00
2000-06-01T00:00:00.000+02:00 and 2000-06-30T00:00:00.000+02:00
2000-07-01T00:00:00.000+02:00 and 2000-07-31T00:00:00.000+02:00
2000-08-01T00:00:00.000+02:00 and 2000-08-31T00:00:00.000+02:00
2000-09-01T00:00:00.000+02:00 and 2000-09-30T00:00:00.000+02:00
2000-10-01T00:00:00.000+02:00 and 2000-10-31T00:00:00.000+01:00
2000-11-01T00:00:00.000+01:00 and 2000-11-30T00:00:00.000+01:00
2000-12-01T00:00:0
Example:
System.out.println("");
System.out.println("*****************************");
System.out.println("Split 2.1.2004 to 26.3.2004: ");
for (DateTime[] date : TimeSplitter.splitDateIntoMonths(new DateTime(2004, 1, 2, 0, 0, 0, 0).toDate(),
new DateTime(2004, 3, 26, 0, 0, 0, 0).toDate())) {
System.out.println(date[0] + " and " + date[1]);
}
System.out.println("");
System.out.println("*****************************");
System.out.println("Split 1.1.2000 to 31.12.2005: ");
for (DateTime[] date : TimeSplitter.splitDateIntoMonths(new DateTime(2000, 1, 1, 0, 0, 0, 0).toDate(),
new DateTime(2005, 12, 31, 0, 0, 0, 0).toDate())) {
System.out.println(date[0] + " and " + date[1]);
}Results into:
```
*****
Split 2.1.2004 to 26.3.2004:
2004-01-02T00:00:00.000+01:00 and 2004-01-31T00:00:00.000+01:00
2004-02-01T00:00:00.000+01:00 and 2004-02-29T00:00:00.000+01:00
2004-03-01T00:00:00.000+01:00 and 2004-03-26T00:00:00.000+01:00
*****
Split 1.1.2000 to 31.12.2005:
2000-01-01T00:00:00.000+01:00 and 2000-01-31T00:00:00.000+01:00
2000-02-01T00:00:00.000+01:00 and 2000-02-29T00:00:00.000+01:00
2000-03-01T00:00:00.000+01:00 and 2000-03-31T00:00:00.000+02:00
2000-04-01T00:00:00.000+02:00 and 2000-04-30T00:00:00.000+02:00
2000-05-01T00:00:00.000+02:00 and 2000-05-31T00:00:00.000+02:00
2000-06-01T00:00:00.000+02:00 and 2000-06-30T00:00:00.000+02:00
2000-07-01T00:00:00.000+02:00 and 2000-07-31T00:00:00.000+02:00
2000-08-01T00:00:00.000+02:00 and 2000-08-31T00:00:00.000+02:00
2000-09-01T00:00:00.000+02:00 and 2000-09-30T00:00:00.000+02:00
2000-10-01T00:00:00.000+02:00 and 2000-10-31T00:00:00.000+01:00
2000-11-01T00:00:00.000+01:00 and 2000-11-30T00:00:00.000+01:00
2000-12-01T00:00:0
Solution
Looks pretty nice overall.
Small bits:
(If you use
Small bits:
- I would create a special type for the
DateTime[](as it can have only exactly 2DateTimes), or reuse an existing one. In joda's caseInterval.
(If you use
Interval, you can also provide a convienence static method that accepts an Interval)- Use a access modifier (
public) on your method
- Replace the
compareTowith better readableisBefore()andisAfter()andequals()so the reader does not need to know the magic compare values.
- Your javadoc has an
mm-dd-YYYYdate format. Your example hasdd.mm.YYYY. I would stick toYYYY-mm-dd
Context
StackExchange Code Review Q#128564, answer score: 2
Revisions (0)
No revisions yet.