Mercurial > dive4elements > framework
changeset 561:1c2ce0501305
Added some methods for Abflussjahr
author | mschaefer |
---|---|
date | Mon, 24 Sep 2018 17:59:17 +0200 |
parents | 2b631f788ce1 |
children | 05caf2e731d0 |
files | artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/DateUtils.java |
diffstat | 1 files changed, 108 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/DateUtils.java Tue Sep 04 16:44:31 2018 +0200 +++ b/artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/DateUtils.java Mon Sep 24 17:59:17 2018 +0200 @@ -17,14 +17,120 @@ * * @return the year as integer or -1 if date is empty. */ - public static int getYearFromDate(Date date) { + public static int getYearFromDate(final Date date) { if (date == null) { return -1; } - Calendar cal = Calendar.getInstance(); + final Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.YEAR); } + + /** + * Gets the abfluss year (1.11. - 31.10.) a date belongs to + * + * @return the abfluss year, or -1 + */ + public static int getAbflussYearFromDate(final Date date) { + if (date == null) + return -1; + final Calendar cal = Calendar.getInstance(); + cal.setTime(getAbflussYear(date)[1]); + return cal.get(Calendar.YEAR); + } + + /** + * Gets the date range of the abfluss year (1.11. - 31.10.) a date belongs to + * + * @return [abfluss year start date, abfluss year end date], or null + */ + public static Date[] getAbflussYear(final Date date) { + if (date == null) + return null; + final Calendar cal = Calendar.getInstance(); + cal.setTime(date); + final int qYear = (cal.get(Calendar.MONTH) >= 10) ? cal.get(Calendar.YEAR) + 1 : cal.get(Calendar.YEAR); + return getAbflussYear(qYear); + } + + /** + * Gets the date range of the abfluss year (1.11. - 31.10.) a date belongs to + * + * @return [abfluss year start date, abfluss year end date], or null + */ + public static Date[] getAbflussYear(final int year) { + final Calendar calStart = Calendar.getInstance(); + calStart.clear(); + calStart.set(year - 1, 10, 1, 0, 0, 0); + final Calendar calEnd = Calendar.getInstance(); + calEnd.clear(); + calEnd.set(year, 9, 31, 23, 59, 59); + return new Date[] { calStart.getTime(), calEnd.getTime() }; + } + + /** + * Gets the date range of the abfluss year following a date, or that of the date itself if it's a nov-1 + * + * @return [abfluss year start date, abfluss year end date], or null + */ + public static Date[] getNextAbflussYear(final Date date) { + if (date == null) + return null; + final Calendar cal = Calendar.getInstance(); + cal.setTime(date); + final int nextQYear = cal.get(Calendar.YEAR) + 1; + switch (cal.get(Calendar.MONTH)) { + case 10: + if (cal.get(Calendar.DAY_OF_MONTH) >= 2) + return getAbflussYear(nextQYear + 1); + break; + case 11: + return getAbflussYear(nextQYear + 1); + default: + break; + } + return getAbflussYear(nextQYear); + } + + /** + * Gets the date range of the abfluss year preceeding a date, or that of the date itself if it's a oct-31 + * + * @return [abfluss year start date, abfluss year end date], or null + */ + public static Date[] getPreviousAbflussYear(final Date date) { + if (date == null) + return null; + final Calendar cal = Calendar.getInstance(); + cal.setTime(date); + final int previousQYear = cal.get(Calendar.YEAR); + switch (cal.get(Calendar.MONTH)) { + case 10: + case 11: + break; + case 9: + if (cal.get(Calendar.DAY_OF_MONTH) <= 30) + return getAbflussYear(previousQYear - 1); + break; + default: + return getAbflussYear(previousQYear - 1); + } + return getAbflussYear(previousQYear); + } + + /** + * Gets the date of the new year's day following a date, or the date itself with 0-time if it's a jan-1 + * + * @return a newyear's date, or null + */ + public static Date getNextNewYear(final Date date) { + if (date == null) + return null; + final Calendar cal = Calendar.getInstance(); + cal.setTime(date); + final int offset = (cal.get(Calendar.DAY_OF_YEAR) == 1) ? 0 : 1; + cal.set(cal.get(Calendar.YEAR) + offset, 0, 1, 0, 0, 0); + return cal.getTime(); + } }