[DS-400] Fix DCDate date formatting for display in JSPUI

git-svn-id: http://scm.dspace.org/svn/repo/dspace/trunk@4662 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Larry Stone
2010-01-08 03:27:08 +00:00
parent c40c9668fc
commit cf0e2ebaa7
3 changed files with 73 additions and 81 deletions

View File

@@ -50,6 +50,8 @@ import java.util.TimeZone;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.core.I18nUtil;
// FIXME: Not very robust - assumes dates will always be valid // FIXME: Not very robust - assumes dates will always be valid
/** /**
@@ -555,6 +557,10 @@ public class DCDate
// display results: // display results:
System.out.println("toString() = \""+d.toString()+"\""); System.out.println("toString() = \""+d.toString()+"\"");
System.out.println("toDate().toString() = \""+d.toDate().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("By component:");
System.out.println("granularity = "+d.granularity); System.out.println("granularity = "+d.granularity);
@@ -581,4 +587,69 @@ public class DCDate
// month str // month str
System.out.println("Month Name = \""+DCDate.getMonthName(d.getMonth(), Locale.getDefault())+"\""); 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());
}
}
} }

View File

@@ -246,88 +246,8 @@ public class UIUtil extends Util
*/ */
public static String displayDate(DCDate d, boolean time, boolean localTime, HttpServletRequest request) public static String displayDate(DCDate d, boolean time, boolean localTime, HttpServletRequest request)
{ {
StringBuffer sb = new StringBuffer(); return d.displayDate(time, localTime, getSessionLocale(request));
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();
} }
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 * Return a string for logging, containing useful information about the

View File

@@ -79,6 +79,7 @@
- [DS-447] Email test script - [DS-447] Email test script
(Larry Stone) (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-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-393] Fix DCDate and submission timezone bugs that changed date on page reload
- [DS-377] Add META tags identifying DSpace source version - [DS-377] Add META tags identifying DSpace source version