1 /*
2 * Oceanus: Java Utilities
3 * Copyright 2012-2026. Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17 package io.github.tonywasher.joceanus.oceanus.date;
18
19 import java.time.Month;
20 import java.util.Locale;
21
22 /**
23 * Fiscal Year representation.
24 */
25 public enum OceanusFiscalYear {
26 /**
27 * Fiscal Year based on UK Model.
28 */
29 UK(6, Month.APRIL),
30
31 /**
32 * March based start of year.
33 */
34 MARCH(Month.MARCH),
35
36 /**
37 * April based start of year.
38 */
39 APRIL(Month.APRIL),
40
41 /**
42 * July based start of year.
43 */
44 JULY(Month.JULY),
45
46 /**
47 * October based start of year.
48 */
49 OCTOBER(Month.OCTOBER),
50
51 /**
52 * Fiscal year based on the CalendarYear.
53 */
54 CALENDAR;
55
56 /**
57 * The day of the first day of fiscal year.
58 */
59 private final int theDay;
60
61 /**
62 * The month of the first day of fiscal year.
63 */
64 private final transient Month theMonth;
65
66 /**
67 * Constructor.
68 */
69 OceanusFiscalYear() {
70 this(Month.JANUARY);
71 }
72
73 /**
74 * Constructor.
75 *
76 * @param pMonth the first month of fiscal year
77 */
78 OceanusFiscalYear(final Month pMonth) {
79 this(1, pMonth);
80 }
81
82 /**
83 * Constructor.
84 *
85 * @param pDay the first day of fiscal year
86 * @param pMonth the first month of fiscal year
87 */
88 OceanusFiscalYear(final int pDay,
89 final Month pMonth) {
90 theDay = pDay;
91 theMonth = pMonth;
92 }
93
94 /**
95 * Obtain the day.
96 *
97 * @return the day
98 */
99 public int getFirstDay() {
100 return theDay;
101 }
102
103 /**
104 * Obtain the month.
105 *
106 * @return the month
107 */
108 public Month getFirstMonth() {
109 return theMonth;
110 }
111
112 /**
113 * Determine Fiscal Year for locale.
114 *
115 * @param pLocale the locale
116 * @return the fiscal year type
117 */
118 public static OceanusFiscalYear determineFiscalYear(final Locale pLocale) {
119 switch (pLocale.getCountry()) {
120 case "GB":
121 return UK;
122 case "ZA":
123 return MARCH;
124 case "NZ":
125 return APRIL;
126 case "AU":
127 case "EG":
128 return JULY;
129 case "CR":
130 return OCTOBER;
131 default:
132 return CALENDAR;
133 }
134 }
135
136 /**
137 * Normalise date to end of FiscalYear.
138 *
139 * @param pDate the date to normalise.
140 * @return the normalised date
141 */
142 public OceanusDate endOfYear(final OceanusDate pDate) {
143 /* Access constituent parts */
144 final int myDay = pDate.getDay();
145 final int myMonth = pDate.getMonth();
146 final int myFiscalMonth = theMonth.getValue();
147
148 /* See whether we are earlier in the year */
149 final boolean bEarlier = myFiscalMonth == myMonth
150 ? myDay < theDay
151 : myMonth < myFiscalMonth;
152
153 /* Build the basic taxYear */
154 final OceanusDate myDate = new OceanusDate(pDate.getYear(), theMonth, theDay, pDate.getLocale());
155
156 /* Adjust if we are later */
157 if (!bEarlier) {
158 myDate.adjustYear(1);
159 }
160
161 /* Move back a day */
162 myDate.adjustDay(-1);
163
164 /* Return the date */
165 return myDate;
166 }
167
168 /**
169 * Normalise date to end of FiscalMonth.
170 *
171 * @param pDate the date to normalise.
172 * @return the normalised date
173 */
174 public OceanusDate endOfMonth(final OceanusDate pDate) {
175 /* Access constituent parts */
176 final int myDay = pDate.getDay();
177
178 /* See whether we are earlier in the month */
179 final boolean bEarlier = myDay < theDay;
180
181 /* Build the basic taxMonth */
182 final OceanusDate myDate = new OceanusDate(pDate.getYear(), pDate.getMonth(), theDay, pDate.getLocale());
183
184 /* Adjust if we are later */
185 if (!bEarlier) {
186 myDate.adjustMonth(1);
187 }
188
189 /* Move back a day */
190 myDate.adjustDay(-1);
191
192 /* Return the date */
193 return myDate;
194 }
195
196 /**
197 * Normalise date to start of FiscalMonth.
198 *
199 * @param pDate the date to normalise.
200 * @return the normalised date
201 */
202 public OceanusDate startOfMonth(final OceanusDate pDate) {
203 /* Access constituent parts */
204 final int myDay = pDate.getDay();
205
206 /* See whether we are earlier in the month */
207 final boolean bEarlier = myDay < theDay;
208
209 /* Build the basic taxMonth */
210 final OceanusDate myDate = new OceanusDate(pDate.getYear(), pDate.getMonth(), theDay, pDate.getLocale());
211
212 /* Adjust if we are earlier */
213 if (bEarlier) {
214 myDate.adjustMonth(-1);
215 }
216
217 /* Return the date */
218 return myDate;
219 }
220 }