# HG changeset patch # User mschaefer # Date 1537804757 -7200 # Node ID 1c2ce0501305e6f8586fd8d1e12da1e632b7a54c # Parent 2b631f788ce1732db9986b8dc7e82c8229f6009b Added some methods for Abflussjahr diff -r 2b631f788ce1 -r 1c2ce0501305 artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/DateUtils.java --- 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(); + } }