diff --git a/dspace-api/src/main/java/org/dspace/content/DCDate.java b/dspace-api/src/main/java/org/dspace/content/DCDate.java index 0d677f96d6..9a82a81b87 100644 --- a/dspace-api/src/main/java/org/dspace/content/DCDate.java +++ b/dspace-api/src/main/java/org/dspace/content/DCDate.java @@ -50,6 +50,8 @@ import java.util.TimeZone; import org.apache.log4j.Logger; +import org.dspace.core.I18nUtil; + // FIXME: Not very robust - assumes dates will always be valid /** @@ -555,6 +557,10 @@ public class DCDate // display results: System.out.println("toString() = \""+d.toString()+"\""); System.out.println("toDate().toString() = \""+d.toDate().toString()+"\""); + System.out.println("\ndisplayDate(time=F,loc=F) = \""+d.displayDate(false, false, I18nUtil.DEFAULTLOCALE)+"\""); + System.out.println("displayDate(time=T,loc=F) = \""+d.displayDate(true, false, I18nUtil.DEFAULTLOCALE)+"\""); + System.out.println("displayDate(time=F,loc=T) = \""+d.displayDate(false, true, I18nUtil.DEFAULTLOCALE)+"\""); + System.out.println("displayDate(time=T,loc=T) = \""+d.displayDate(true, true, I18nUtil.DEFAULTLOCALE)+"\""); System.out.println("By component:"); System.out.println("granularity = "+d.granularity); @@ -581,4 +587,69 @@ public class DCDate // month str System.out.println("Month Name = \""+DCDate.getMonthName(d.getMonth(), Locale.getDefault())+"\""); } + + /** + * Format a human-readable version of the DCDate, with optional time. + * This needs to be in DCDate because it depends on the granularity of + * the original time. + * + * FIXME: This should probably be replaced with a localized DateFormat. + * + * @param showTime + * if true, display the time with the date + * @param isLocalTime + * if true, adjust for local time zone, otherwise GMT + * @param locale + * locale of the user + * + * @return String with the date in a human-readable form. + */ + public String displayDate(boolean showTime, boolean isLocalTime, Locale locale) + { + // if we are only showing day of a DCDate with time granularity, + // create a temporary DCDate with date granularity so getDay() etc work. + DCDate dd = this; + if (!showTime && granularity == DateGran.TIME) + { + dd = new DCDate(); + dd.setDateLocal(getYearGMT(), getMonthGMT(), getDayGMT(), -1, -1, -1); + } + + // forcibly truncate month name to 3 chars -- XXX FIXME? + String monthName = DCDate.getMonthName(dd.getMonth(), locale); + if (monthName.length() > 2) + monthName = monthName.substring(0, 3); + + // display date and time + if (showTime && granularity == DateGran.TIME) + { + if (isLocalTime) + { + return String.format("%d-%s-%4d %02d:%02d:%02d", + dd.getDay(), monthName, dd.getYear(), + dd.getHour(), dd.getMinute(), dd.getSecond()); + } + else + { + monthName = DCDate.getMonthName(dd.getMonthGMT(), locale); + if (monthName.length() > 2) + monthName = monthName.substring(0, 3); + return String.format("%d-%s-%4d %02d:%02d:%02d", + dd.getDayGMT(), monthName, dd.getYearGMT(), + dd.getHourGMT(), dd.getMinuteGMT(), dd.getSecondGMT()); + } + } + else if (granularity == DateGran.DAY) + { + return String.format("%d-%s-%4d", dd.getDay(), monthName, dd.getYear()); + } + else if (granularity == DateGran.MONTH) + { + return String.format("%s-%4d", monthName, dd.getYear()); + } + else + { + return String.format("%4d", dd.getYear()); + } + } } diff --git a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/UIUtil.java b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/UIUtil.java index 5e82598e12..7e9c4611da 100644 --- a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/UIUtil.java +++ b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/UIUtil.java @@ -246,88 +246,8 @@ public class UIUtil extends Util */ public static String displayDate(DCDate d, boolean time, boolean localTime, HttpServletRequest request) { - StringBuffer sb = new StringBuffer(); - Locale locale = ((Context)request.getAttribute("dspace.context")).getCurrentLocale(); - if (locale == null) locale = I18nUtil.DEFAULTLOCALE; - - if (d != null) - { - int year; - int month; - int day; - int hour; - int minute; - int second; - - if (localTime) - { - year = d.getYear(); - month = d.getMonth(); - day = d.getDay(); - hour = d.getHour(); - minute = d.getMinute(); - second = d.getSecond(); + return d.displayDate(time, localTime, getSessionLocale(request)); } - else - { - year = d.getYearGMT(); - month = d.getMonthGMT(); - day = d.getDayGMT(); - hour = d.getHourGMT(); - minute = d.getMinuteGMT(); - second = d.getSecondGMT(); - } - - if (year > -1) - { - if (month > -1) - { - if (day > -1) - { - sb.append(day + "-"); - } - String monthName = DCDate.getMonthName(month, getSessionLocale(request)); - int monthLength = monthName.length(); - monthLength = monthLength > 2 ? 3 : monthLength; - sb.append(monthName.substring(0, monthLength) + "-"); - } - - sb.append(year + " "); - } - - if (time && (hour > -1)) - { - String hr = String.valueOf(hour); - - while (hr.length() < 2) - { - hr = "0" + hr; - } - - String mn = String.valueOf(minute); - - while (mn.length() < 2) - { - mn = "0" + mn; - } - - String sc = String.valueOf(second); - - while (sc.length() < 2) - { - sc = "0" + sc; - } - - sb.append(hr + ":" + mn + ":" + sc + " "); - } - } - else - { - sb.append("Unset"); - } - - return (sb.toString()); - } /** * Return a string for logging, containing useful information about the diff --git a/dspace/CHANGES b/dspace/CHANGES index fc831ab7e9..bd6cdd57b9 100644 --- a/dspace/CHANGES +++ b/dspace/CHANGES @@ -79,6 +79,7 @@ - [DS-447] Email test script (Larry Stone) + - [DS-400] Fix DCDate date formatting for display in JSPUI (thanks also to Stuart Lewis for testing and final adjustment) - [DS-284] alternate odd and even styles correctly on table rows in Item Summary view - [DS-393] Fix DCDate and submission timezone bugs that changed date on page reload - [DS-377] Add META tags identifying DSpace source version