From 578ad0d6bc408edf9e6c875156054374f5fd8337 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Mon, 22 Mar 2021 18:40:26 +0100 Subject: change to EGIZ codestyle --- .../gv/egovernment/moaspss/util/DateTimeUtils.java | 344 +++++++++++---------- 1 file changed, 179 insertions(+), 165 deletions(-) (limited to 'moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java') diff --git a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java index c199c29..a874bd5 100644 --- a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java +++ b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java @@ -21,7 +21,6 @@ * that you distribute must include a readable copy of the "NOTICE" text file. */ - package at.gv.egovernment.moaspss.util; import java.io.StringWriter; @@ -38,9 +37,9 @@ import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; /** - * Utility for parsing and building XML type dateTime, - * according to ISO 8601. - * + * Utility for parsing and building XML type dateTime, according to + * ISO 8601. + * * @author Patrick Peck * @version $Id$ * @see http://www.w3.org/2001/XMLSchema-datatypes" @@ -55,7 +54,7 @@ public class DateTimeUtils { // * @return the dateTime value // */ // public static String buildDateTime(Calendar cal, boolean useUTC) { -// +// // if (useUTC) // return buildDateTimeUTC(cal); // else { @@ -92,41 +91,46 @@ public class DateTimeUtils { // return out.toString(); // } // } - + /** - * Builds a dateTime value in UTC from a Calendar value. + * Builds a dateTime value in UTC from a Calendar + * value. + * * @param cal the Calendar value * @return the dateTime value */ public static String buildDateTimeUTC(Calendar cal) { - - SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - f.setTimeZone(TimeZone.getTimeZone("UTC")); - - return f.format(cal.getTime()); + + final SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + f.setTimeZone(TimeZone.getTimeZone("UTC")); + + return f.format(cal.getTime()); } - + /** - * Builds a dateTime value in UTC from a Calendar value. + * Builds a dateTime value in UTC from a Calendar + * value. + * * @param cal the Calendar value * @return the dateTime value */ public static String buildDateTimeUTC(Date cal) { - - SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - f.setTimeZone(TimeZone.getTimeZone("UTC")); - - return f.format(cal); - + + final SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + f.setTimeZone(TimeZone.getTimeZone("UTC")); + + return f.format(cal); + } - + /** * Builds a dateTime value from a Calendar value. + * * @param cal the Calendar value * @return the dateTime value */ public static String buildDate(Calendar cal) { - StringWriter out = new StringWriter(); + final StringWriter out = new StringWriter(); out.write("" + cal.get(Calendar.YEAR)); out.write("-"); out.write(to2DigitString(cal.get(Calendar.MONTH) + 1)); @@ -134,43 +138,45 @@ public class DateTimeUtils { out.write(to2DigitString(cal.get(Calendar.DAY_OF_MONTH))); return out.toString(); } - + /** * Builds a dateTime value from a Calendar value. + * * @param cal the Calendar value * @return the dateTime value */ public static String buildTime(Calendar cal) { - StringWriter out = new StringWriter(); - out.write(to2DigitString(cal.get(Calendar.HOUR_OF_DAY))); - out.write(":"); - out.write(to2DigitString(cal.get(Calendar.MINUTE))); - out.write(":"); - out.write(to2DigitString(cal.get(Calendar.SECOND))); - - return out.toString(); + final StringWriter out = new StringWriter(); + out.write(to2DigitString(cal.get(Calendar.HOUR_OF_DAY))); + out.write(":"); + out.write(to2DigitString(cal.get(Calendar.MINUTE))); + out.write(":"); + out.write(to2DigitString(cal.get(Calendar.SECOND))); + + return out.toString(); } - + /** - * Converts month, day, hour, minute, or second value - * to a 2 digit String. + * Converts month, day, hour, minute, or second value to a 2 digit String. + * * @param number the month, day, hour, minute, or second value * @return 2 digit String */ private static String to2DigitString(int number) { - if (number < 10) + if (number < 10) { return "0" + number; - else + } else { return "" + number; + } } /** - * Parse a String containing a date and time instant, given in - * ISO 8601 format. - * + * Parse a String containing a date and time instant, given in ISO + * 8601 format. + * * @param dateTime The String to parse. * @return The Date representation of the contents of - * dateTime. + * dateTime. * @throws ParseException Parsing the dateTime failed. */ public static Date parseDateTime(String dateTime) throws ParseException { @@ -247,8 +253,7 @@ public class DateTimeUtils { curPos++; ensureDigits(dateTime, curPos, 1); fractStr = "0."; - fractStr - += dateTime.substring(curPos, curPos + countDigits(dateTime, curPos)); + fractStr += dateTime.substring(curPos, curPos + countDigits(dateTime, curPos)); fraction = Double.parseDouble(fractStr); curPos += countDigits(dateTime, curPos); } @@ -294,12 +299,12 @@ public class DateTimeUtils { calendar.set(Calendar.MILLISECOND, 0); time = calendar.getTime().getTime(); time += (long) (fraction * 1000.0); - time -= tzSign * ((tzHour * 60) + tzMinute) * 60 * 1000; + time -= tzSign * (tzHour * 60 + tzMinute) * 60 * 1000; if (localTime) { - time -= TimeZone.getDefault().getRawOffset(); + time -= TimeZone.getDefault().getRawOffset(); } return new Date(time); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { throw new ParseException(msg.getMessage("datetime.00", null), curPos); } @@ -307,16 +312,16 @@ public class DateTimeUtils { /** * Parse an integer value. - * - * @param str The String containing the digits. + * + * @param str The String containing the digits. * @param curPos The starting position. * @param digits The number of digist making up the integer value. * @return int The integer representation of the digits contained in - * str. + * str. * @throws ParseException Parsing the integer value failed. */ private static int parseInt(String str, int curPos, int digits) - throws ParseException { + throws ParseException { ensureDigits(str, curPos, digits); return Integer.parseInt(str.substring(curPos, curPos + digits)); @@ -324,31 +329,33 @@ public class DateTimeUtils { /** * Count the number of digits following curPos. - * - * @param str The String in which to count digits. + * + * @param str The String in which to count digits. * @param curPos The starting position. * @return int The number of digits. */ private static int countDigits(String str, int curPos) { int i; - for (i = curPos; i < str.length() && Character.isDigit(str.charAt(i)); i++); + for (i = curPos; i < str.length() && Character.isDigit(str.charAt(i)); i++) { + ; + } return i - curPos; } /** * Ensure that a value falls in a given min/max range. - * - * @param value The value to check. - * @param min The minimum allowed value. - * @param max The maximum allowed value. + * + * @param value The value to check. + * @param min The minimum allowed value. + * @param max The maximum allowed value. * @param curPos To indicate the parsing position in the - * ParseException. + * ParseException. * @throws ParseException Thrown, if value < min || value > * max */ private static void ensureValue(int value, int min, int max, int curPos) - throws ParseException { + throws ParseException { if (value < min || value > max) { throw new ParseException(msg.getMessage("datetime.00", null), curPos); @@ -357,16 +364,16 @@ public class DateTimeUtils { /** * Ensure that the given String has a number of characters left. - * - * @param str The String to check for its length. + * + * @param str The String to check for its length. * @param curPos The starting position. - * @param count The minimum number of characters that str must - * contain, starting at from curPos. - * @throws ParseException Thrown, if - * curPos + count > str.length(). + * @param count The minimum number of characters that str must + * contain, starting at from curPos. + * @throws ParseException Thrown, if + * curPos + count > str.length(). */ private static void ensureChars(String str, int curPos, int count) - throws ParseException { + throws ParseException { if (curPos + count > str.length()) { throw new ParseException(msg.getMessage("datetime.00", null), curPos); } @@ -375,17 +382,17 @@ public class DateTimeUtils { /** * Ensure that a given String contains a certain character at a * certain position. - * - * @param str The String in which to look up the character. + * + * @param str The String in which to look up the character. * @param curPos The position in str that must contain the - * character. - * @param c The character value that must be contained at position - * curPos. + * character. + * @param c The character value that must be contained at position + * curPos. * @throws ParseException Thrown, if the characters do not match or - * curPos is out of range. + * curPos is out of range. */ private static void ensureChar(String str, int curPos, char c) - throws ParseException { + throws ParseException { ensureChars(str, curPos, 1); if (str.charAt(curPos) != c) { @@ -394,19 +401,19 @@ public class DateTimeUtils { } /** - * Ensure that a given String contains a number of digits, - * starting at a given position. - * - * @param str The String to scan for digits. + * Ensure that a given String contains a number of digits, starting + * at a given position. + * + * @param str The String to scan for digits. * @param curPos The starting postion. - * @param count The number of digits that must be contained in - * str, starting at curPos. - * @throws ParseException Thrown, if str is not long enough, or - * one of the characters following curPos in str is - * not a digit. + * @param count The number of digits that must be contained in + * str, starting at curPos. + * @throws ParseException Thrown, if str is not long enough, or one + * of the characters following curPos in + * str is not a digit. */ private static void ensureDigits(String str, int curPos, int count) - throws ParseException { + throws ParseException { ensureChars(str, curPos, count); for (int i = curPos; i < curPos + count; i++) { @@ -415,101 +422,108 @@ public class DateTimeUtils { } } } - + /** * Calculates the age if date of birth is given (for a calendar time stamp) + * * @param dateOfBirth Date of Birth - * @param now Calendar time stamp at which the age needs to be calculated for + * @param now Calendar time stamp at which the age needs to be + * calculated for * @return Age of a person */ public static int calcAge(Calendar dateOfBirth, Calendar now) { - int age = now.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR); - - int nowM = now.get(Calendar.MONTH); - int dobM = dateOfBirth.get(Calendar.MONTH); - int nowDOM = now.get(Calendar.DAY_OF_MONTH); - int dobDOM = dateOfBirth.get(Calendar.DAY_OF_MONTH); - - if ((nowM < dobM) || ((nowM == dobM) && (nowDOM < dobDOM))) { - age--; - } - - if (age < 0) { - throw new IllegalArgumentException("Calculated age results in negative value."); - } - return age; - } + int age = now.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR); + + final int nowM = now.get(Calendar.MONTH); + final int dobM = dateOfBirth.get(Calendar.MONTH); + final int nowDOM = now.get(Calendar.DAY_OF_MONTH); + final int dobDOM = dateOfBirth.get(Calendar.DAY_OF_MONTH); + + if (nowM < dobM || nowM == dobM && nowDOM < dobDOM) { + age--; + } + + if (age < 0) { + throw new IllegalArgumentException("Calculated age results in negative value."); + } + return age; + } /** * Calculates the age if date of birth is given as Calendar object + * * @param dateOfBirth Date of Birth as Calendar object * @return Age of a person */ - public static int calcAge(Calendar dateOfBirth) { - return calcAge(dateOfBirth, Calendar.getInstance()); - } - - /** - * Calculates the age if date of birth is given (for a date time stamp) - * @param dateOfBirth Date of Birth - * @param now Date time stamp at which the age needs to be calculated for - * @return Age of a person - */ - public static int calcAge(Date dateOfBirth, Date now) { - Calendar dob = Calendar.getInstance(); - dob.setTime(dateOfBirth); - Calendar nowCal = Calendar.getInstance(); - nowCal.setTime(now); - return calcAge(dob, nowCal); - } - - /** - * Calculates the age if date of birth is given as Date object - * @param dateOfBirth Date of Birth as Date object - * @return Age of a person - */ - public static int calcAge(Date dateOfBirth) { - return calcAge(dateOfBirth, new Date()); - } - - public static String formatPEPSDateToMOADate(String pepsDate) { - - if (StringUtils.isEmpty(pepsDate)) { - return null; - } - - DateTimeFormatter fmt = null; - - switch (pepsDate.length()) { - case 4: - fmt = DateTimeFormat.forPattern("yyyy"); - break; - case 6: - fmt = DateTimeFormat.forPattern("yyyyMM"); - break; - case 8: - fmt = DateTimeFormat.forPattern("yyyyMMdd"); - break; - default: - fmt = DateTimeFormat.forPattern("yyyy-MM-dd"); - break; - } - - DateTime dt = fmt.parseDateTime(pepsDate); - DateTimeFormatter fmt2 = DateTimeFormat.forPattern("yyyy-MM-dd"); - return fmt2.print(dt); - - } - - /** - * Returns a date as String using a provided format - * @param format Format the date/time should be returned - * @return Date/Time as String formatted according the provided format - */ - public static String getDateTimeWithFormat(String format) { - DateFormat dateFormat = new SimpleDateFormat(format); - Date date = new Date(); - return dateFormat.format(date); + public static int calcAge(Calendar dateOfBirth) { + return calcAge(dateOfBirth, Calendar.getInstance()); + } + + /** + * Calculates the age if date of birth is given (for a date time stamp) + * + * @param dateOfBirth Date of Birth + * @param now Date time stamp at which the age needs to be calculated + * for + * @return Age of a person + */ + public static int calcAge(Date dateOfBirth, Date now) { + final Calendar dob = Calendar.getInstance(); + dob.setTime(dateOfBirth); + final Calendar nowCal = Calendar.getInstance(); + nowCal.setTime(now); + return calcAge(dob, nowCal); + } + + /** + * Calculates the age if date of birth is given as Date object + * + * @param dateOfBirth Date of Birth as Date object + * @return Age of a person + */ + public static int calcAge(Date dateOfBirth) { + return calcAge(dateOfBirth, new Date()); + } + + public static String formatPEPSDateToMOADate(String pepsDate) { + + if (StringUtils.isEmpty(pepsDate)) { + return null; } + DateTimeFormatter fmt = null; + + switch (pepsDate.length()) { + case 4: + fmt = DateTimeFormat.forPattern("yyyy"); + break; + case 6: + fmt = DateTimeFormat.forPattern("yyyyMM"); + break; + case 8: + fmt = DateTimeFormat.forPattern("yyyyMMdd"); + break; + default: + fmt = DateTimeFormat.forPattern("yyyy-MM-dd"); + break; + } + + final DateTime dt = fmt.parseDateTime(pepsDate); + final DateTimeFormatter fmt2 = DateTimeFormat.forPattern("yyyy-MM-dd"); + return fmt2.print(dt); + + } + + /** + * Returns a date as String using a provided format + * + * @param format Format the date/time should be returned + * @return Date/Time as String formatted according the provided format + */ + public static String getDateTimeWithFormat(String format) { + final DateFormat dateFormat = new SimpleDateFormat(format); + final Date date = new Date(); + return dateFormat.format(date); + } + } -- cgit v1.2.3