MCPcopy Create free account
hub / github.com/MenoData/Time4J / fromEpochMonths

Method fromEpochMonths

base/src/main/java/net/time4j/PlainDate.java:1985–2050  ·  view source on GitHub ↗
(
        PlainDate context,
        long emonths,
        int dayOfMonth,
        int policy
    )

Source from the content-addressed store, hash-verified

1983 }
1984
1985 private static PlainDate fromEpochMonths(
1986 PlainDate context,
1987 long emonths,
1988 int dayOfMonth,
1989 int policy
1990 ) {
1991
1992 if (
1993 (policy == OverflowUnit.POLICY_KEEPING_LAST_DATE)
1994 && (context.dayOfMonth == context.lengthOfMonth())
1995 ) {
1996 policy = OverflowUnit.POLICY_END_OF_MONTH;
1997 }
1998
1999 int year =
2000 MathUtils.safeCast(
2001 MathUtils.safeAdd(
2002 MathUtils.floorDivide(emonths, 12),
2003 1970
2004 )
2005 );
2006 int month = MathUtils.floorModulo(emonths, 12) + 1;
2007 int max = GregorianMath.getLengthOfMonth(year, month);
2008 int dom = dayOfMonth;
2009
2010 if (dayOfMonth > max) {
2011 switch (policy) {
2012 case OverflowUnit.POLICY_PREVIOUS_VALID_DATE:
2013 case OverflowUnit.POLICY_END_OF_MONTH:
2014 case OverflowUnit.POLICY_KEEPING_LAST_DATE:
2015 case OverflowUnit.POLICY_JODA_METRIC:
2016 dom = max;
2017 break;
2018 case OverflowUnit.POLICY_NEXT_VALID_DATE:
2019 return PlainDate.fromEpochMonths(
2020 context,
2021 MathUtils.safeAdd(emonths, 1),
2022 1,
2023 policy);
2024 case OverflowUnit.POLICY_CARRY_OVER:
2025 return PlainDate.fromEpochMonths(
2026 context,
2027 MathUtils.safeAdd(emonths, 1),
2028 dayOfMonth - max,
2029 policy);
2030 case OverflowUnit.POLICY_UNLESS_INVALID:
2031 StringBuilder sb = new StringBuilder(32);
2032 sb.append("Day of month out of range: ");
2033 formatYear(sb, year);
2034 format2Digits(sb, month);
2035 format2Digits(sb, dayOfMonth);
2036 throw new ChronoException(sb.toString());
2037 default:
2038 throw new UnsupportedOperationException(
2039 "Overflow policy not implemented: " + policy);
2040 }
2041 } else if(
2042 (dayOfMonth < max)

Callers 1

doAddMethod · 0.95

Calls 10

safeCastMethod · 0.95
safeAddMethod · 0.95
floorDivideMethod · 0.95
floorModuloMethod · 0.95
getLengthOfMonthMethod · 0.95
formatYearMethod · 0.95
format2DigitsMethod · 0.95
ofMethod · 0.95
lengthOfMonthMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected