From 04116bb8fc1e56ab992994a45dc935c9d2af879c Mon Sep 17 00:00:00 2001 From: dfurrer <none@none> Date: Fri, 4 Mar 2016 12:59:40 +0100 Subject: [PATCH] OO-1926: added formaDateRelative to formatter to add a specific period of time to a baseline date and format it short. made it available in the phantomJS certificates --- .../java/org/olat/core/util/Formatter.java | 19 +++++++++++++++ .../manager/CertificatePhantomWorker.java | 3 +++ .../org/olat/core/util/FormatterTest.java | 24 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/main/java/org/olat/core/util/Formatter.java b/src/main/java/org/olat/core/util/Formatter.java index 3c4af27bf59..778f7c48ac6 100644 --- a/src/main/java/org/olat/core/util/Formatter.java +++ b/src/main/java/org/olat/core/util/Formatter.java @@ -32,6 +32,10 @@ import java.net.URLEncoder; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Period; +import java.time.ZoneId; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -132,6 +136,21 @@ public class Formatter { } } + /** + * adds the given period in day/month/years to the baseLineDate and formats it in a short format, e.g. 05.12.2015 or 12/05/2015 + * + * @param baseLineDate the date + * @return a String with the formatted date + */ + public String formatDateRelative(Date baseLineDate, int days, int months, int years) { + if (baseLineDate == null) return null; + LocalDate date = LocalDateTime.ofInstant(baseLineDate.toInstant(),ZoneId.systemDefault()).toLocalDate(); + Period period = Period.of(years, months, days); + LocalDate relativeDate = date.plus(period); + Date result = Date.from(relativeDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); + return formatDate(result); + } + /** * Formats the given date in a medium sized format, e.g. 12. Dezember 2015 or December 12, 2015 * diff --git a/src/main/java/org/olat/course/certificate/manager/CertificatePhantomWorker.java b/src/main/java/org/olat/course/certificate/manager/CertificatePhantomWorker.java index 6dbeba37aa2..2c938cbeca4 100644 --- a/src/main/java/org/olat/course/certificate/manager/CertificatePhantomWorker.java +++ b/src/main/java/org/olat/course/certificate/manager/CertificatePhantomWorker.java @@ -210,6 +210,7 @@ public class CertificatePhantomWorker { private void fillCertificationInfos(VelocityContext context) { Formatter format = Formatter.getInstance(locale); + context.put("dateFormatter", format); if(dateCertification == null) { context.put("dateCertification", ""); @@ -218,6 +219,7 @@ public class CertificatePhantomWorker { context.put("dateCertification", formattedDateCertification); String formattedDateCertificationLong= format.formatDateLong(dateCertification); context.put("dateCertificationLong", formattedDateCertificationLong); + context.put("dateCertificationRaw", dateCertification); } if(dateFirstCertification == null) { @@ -227,6 +229,7 @@ public class CertificatePhantomWorker { context.put("dateFirstCertification", formattedDateFirstCertification); String formattedDateFirstCertificationLong = format.formatDate(dateFirstCertification); context.put("dateFirstCertificationLong", formattedDateFirstCertificationLong); + context.put("dateFirstCertificationRaw", dateFirstCertification); } } diff --git a/src/test/java/org/olat/core/util/FormatterTest.java b/src/test/java/org/olat/core/util/FormatterTest.java index c403f086922..57e07a5d505 100644 --- a/src/test/java/org/olat/core/util/FormatterTest.java +++ b/src/test/java/org/olat/core/util/FormatterTest.java @@ -19,6 +19,9 @@ */ package org.olat.core.util; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Locale; import org.apache.commons.lang.StringEscapeUtils; import org.junit.Assert; import org.junit.Test; @@ -56,4 +59,25 @@ public class FormatterTest { String ugly = "guido/\\:? .|*\"\"<><guidoöäü"; Assert.assertEquals("guido%2F%5C%3A%3F+.%7C*%22%22%3C%3E%3Cguido%C3%B6%C3%A4%C3%BC", Formatter.makeStringFilesystemSave(ugly)); } + + @Test + public void testDateRelative() { + //zero to add + Formatter formatter = Formatter.getInstance(Locale.GERMAN); + Date base = new GregorianCalendar(1935, 2, 29).getTime(); + Assert.assertEquals(formatter.formatDate(base), formatter.formatDateRelative(base, 0,0,0)); + //add 3 years in the past + Date basePlusThreeY = new GregorianCalendar(1938, 2, 29).getTime(); + Assert.assertEquals(formatter.formatDate(basePlusThreeY), formatter.formatDateRelative(base, 0,0,3)); + //add 5 days at 29 feb (leap year) + base = new GregorianCalendar(2016, 1, 29).getTime(); + Date basePlusFiveD = new GregorianCalendar(2016, 2, 5).getTime(); + Assert.assertEquals(formatter.formatDate(basePlusFiveD), formatter.formatDateRelative(base, 5,0,0)); + //add three moth + base = new GregorianCalendar(2016, 4, 15).getTime(); + Date baseThreeM = new GregorianCalendar(2016, 7, 15).getTime(); + Assert.assertEquals(formatter.formatDate(baseThreeM), formatter.formatDateRelative(base, 0,3,0)); + } + + } -- GitLab