-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This makes the app free from months count assumption thus making it more flexible for other kinds of calendars.
- Loading branch information
Showing
5 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/io/github/persiancalendar/calendar/YearMonthDate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.github.persiancalendar.calendar; | ||
|
||
interface YearMonthDate<T extends AbstractDate> { | ||
// Ideally getYear()/getMonth()/getDay() also should be moved to this interface | ||
|
||
T monthStartOfMonthsDistance(int monthsDistance); | ||
|
||
int monthsDistanceTo(T date); | ||
|
||
interface CreateDate<T extends AbstractDate> { | ||
T createDate(int year, int month, int dayOfMonth); | ||
} | ||
|
||
class TwelveMonthsYear { | ||
static <T extends AbstractDate> T monthStartOfMonthsDistance( | ||
T baseDate, int monthsDistance, CreateDate<T> createDate | ||
) { | ||
int month = monthsDistance + baseDate.getMonth() - 1; // make it zero based for easier calculations | ||
int year = baseDate.getYear() + month / 12; | ||
month %= 12; | ||
if (month < 0) { | ||
year -= 1; | ||
month += 12; | ||
} | ||
return createDate.createDate(year, month + 1, 1); | ||
} | ||
|
||
static <T extends AbstractDate> int monthsDistanceTo(T baseDate, T toDate) { | ||
return (toDate.getYear() - baseDate.getYear()) * 12 + toDate.getMonth() - baseDate.getMonth(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters