aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java
diff options
context:
space:
mode:
authorkstranacher_eGovL <kstranacher_eGovL@d688527b-c9ab-4aba-bd8d-4036d912da1d>2012-07-13 10:15:53 +0000
committerkstranacher_eGovL <kstranacher_eGovL@d688527b-c9ab-4aba-bd8d-4036d912da1d>2012-07-13 10:15:53 +0000
commit07b0306ca470cca10eecceab1a762f995b894fb0 (patch)
treef93b4dcb1e6b14ca97698d862587a49a0f28da47 /common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java
parent1626ac9867cd5406b83e73651080e33c11fb98d1 (diff)
downloadmoa-id-spss-07b0306ca470cca10eecceab1a762f995b894fb0.tar.gz
moa-id-spss-07b0306ca470cca10eecceab1a762f995b894fb0.tar.bz2
moa-id-spss-07b0306ca470cca10eecceab1a762f995b894fb0.zip
Integration of STORK
git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/trunk@1286 d688527b-c9ab-4aba-bd8d-4036d912da1d
Diffstat (limited to 'common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java')
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java100
1 files changed, 100 insertions, 0 deletions
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java b/common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java
index d70073db8..8d57f911a 100644
--- a/common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java
+++ b/common/src/main/java/at/gv/egovernment/moa/util/DateTimeUtils.java
@@ -25,6 +25,7 @@
package at.gv.egovernment.moa.util;
import java.io.StringWriter;
+import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -32,6 +33,10 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
+import org.joda.time.DateTime;
+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.
@@ -396,5 +401,100 @@ 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
+ * @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;
+ }
+
+ /**
+ * 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:
+ 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);
+ }
}