aboutsummaryrefslogtreecommitdiff
path: root/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java')
-rw-r--r--moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/DateTimeUtils.java344
1 files changed, 179 insertions, 165 deletions
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 <code>dateTime</code>,
- * according to ISO 8601.
- *
+ * Utility for parsing and building XML type <code>dateTime</code>, according to
+ * ISO 8601.
+ *
* @author Patrick Peck
* @version $Id$
* @see <code>http://www.w3.org/2001/XMLSchema-datatypes"</code>
@@ -55,7 +54,7 @@ public class DateTimeUtils {
// * @return the <code>dateTime</code> 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 <code>dateTime</code> value in UTC from a <code>Calendar</code> value.
+ * Builds a <code>dateTime</code> value in UTC from a <code>Calendar</code>
+ * value.
+ *
* @param cal the <code>Calendar</code> value
* @return the <code>dateTime</code> 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 <code>dateTime</code> value in UTC from a <code>Calendar</code> value.
+ * Builds a <code>dateTime</code> value in UTC from a <code>Calendar</code>
+ * value.
+ *
* @param cal the <code>Calendar</code> value
* @return the <code>dateTime</code> 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 <code>dateTime</code> value from a <code>Calendar</code> value.
+ *
* @param cal the <code>Calendar</code> value
* @return the <code>dateTime</code> 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 <code>dateTime</code> value from a <code>Calendar</code> value.
+ *
* @param cal the <code>Calendar</code> value
* @return the <code>dateTime</code> 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 <code>String</code> containing a date and time instant, given in
- * ISO 8601 format.
- *
+ * Parse a <code>String</code> containing a date and time instant, given in ISO
+ * 8601 format.
+ *
* @param dateTime The <code>String</code> to parse.
* @return The <code>Date</code> representation of the contents of
- * <code>dateTime</code>.
+ * <code>dateTime</code>.
* @throws ParseException Parsing the <code>dateTime</code> 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 <code>String</code> containing the digits.
+ *
+ * @param str The <code>String</code> 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
- * <code>str</code>.
+ * <code>str</code>.
* @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 <code>curPos</code>.
- *
- * @param str The <code>String</code> in which to count digits.
+ *
+ * @param str The <code>String</code> 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
- * <code>ParseException</code>.
+ * <code>ParseException</code>.
* @throws ParseException Thrown, if <code>value &lt; min || value &gt;
* max</code>
*/
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 <code>String</code> has a number of characters left.
- *
- * @param str The <code>String</code> to check for its length.
+ *
+ * @param str The <code>String</code> to check for its length.
* @param curPos The starting position.
- * @param count The minimum number of characters that <code>str</code> must
- * contain, starting at from <code>curPos</code>.
- * @throws ParseException Thrown, if
- * <code>curPos + count &gt; str.length()</code>.
+ * @param count The minimum number of characters that <code>str</code> must
+ * contain, starting at from <code>curPos</code>.
+ * @throws ParseException Thrown, if
+ * <code>curPos + count &gt; str.length()</code>.
*/
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 <code>String</code> contains a certain character at a
* certain position.
- *
- * @param str The <code>String</code> in which to look up the character.
+ *
+ * @param str The <code>String</code> in which to look up the character.
* @param curPos The position in <code>str</code> that must contain the
- * character.
- * @param c The character value that must be contained at position
- * <code>curPos</code>.
+ * character.
+ * @param c The character value that must be contained at position
+ * <code>curPos</code>.
* @throws ParseException Thrown, if the characters do not match or
- * <code>curPos</code> is out of range.
+ * <code>curPos</code> 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 <code>String</code> contains a number of digits,
- * starting at a given position.
- *
- * @param str The <code>String</code> to scan for digits.
+ * Ensure that a given <code>String</code> contains a number of digits, starting
+ * at a given position.
+ *
+ * @param str The <code>String</code> to scan for digits.
* @param curPos The starting postion.
- * @param count The number of digits that must be contained in
- * <code>str</code>, starting at <code>curPos</code>.
- * @throws ParseException Thrown, if <code>str</code> is not long enough, or
- * one of the characters following <code>curPos</code> in <code>str</code> is
- * not a digit.
+ * @param count The number of digits that must be contained in
+ * <code>str</code>, starting at <code>curPos</code>.
+ * @throws ParseException Thrown, if <code>str</code> is not long enough, or one
+ * of the characters following <code>curPos</code> in
+ * <code>str</code> 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);
+ }
+
}