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 4aa3fab27a..b5fcf9aed4 100644 --- a/dspace-api/src/main/java/org/dspace/content/DCDate.java +++ b/dspace-api/src/main/java/org/dspace/content/DCDate.java @@ -58,7 +58,8 @@ import org.dspace.core.I18nUtil; * Dublin Core date utility class *

* Dates in the DSpace database are held in the ISO 8601 format. They are always - * stored in UTC, converting to and from the current time zone. + * stored in UTC, converting to and from the current time zone. In practice only dates + * with a time component need to be converted. *

* YYYY-MM-DDThh:mm:ss *

@@ -68,10 +69,6 @@ import org.dspace.core.I18nUtil; * Examples: 1994-05-03T15:30:24,1995-10-04, * 2001-10,1975 * - * The main() method is a simple test program: run it with an optional - * first argument that is a date string to decode, and it prints the - * results of all the accessor methods. - * * @author Robert Tansley * @author Larry Stone * @version $Revision$ @@ -84,8 +81,14 @@ public class DCDate // UTC timezone private static final TimeZone utcZone = TimeZone.getTimeZone("UTC"); - // local timezone - private static final TimeZone localZone = new GregorianCalendar().getTimeZone(); + // components of time in UTC + private GregorianCalendar calendar = null; + + // components of time in local zone + private GregorianCalendar localCalendar = null; + + private enum DateGran { YEAR, MONTH, DAY, TIME } + DateGran granularity = null; // Full ISO 8601 is e.g. "2009-07-16T13:59:21Z" private static final SimpleDateFormat fullIso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); @@ -99,6 +102,10 @@ public class DCDate private static final SimpleDateFormat fullIso3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"); static { fullIso3.setTimeZone(utcZone); } + // without minutes + private static final SimpleDateFormat fullIso4 = new SimpleDateFormat("yyyy-MM-dd'T'HH"); + static { fullIso4.setTimeZone(utcZone); } + // Date-only ISO 8601 is e.g. "2009-07-16" private static final SimpleDateFormat dateIso = new SimpleDateFormat("yyyy-MM-dd"); static { dateIso.setTimeZone(utcZone); } @@ -111,33 +118,16 @@ public class DCDate private static final SimpleDateFormat yearIso = new SimpleDateFormat("yyyy"); static { yearIso.setTimeZone(utcZone); } - // components of time in UTC - private GregorianCalendar calendar = null; - - // components of time in local zone, if needed - private GregorianCalendar localCalendar = null; - - private enum DateGran { YEAR, MONTH, DAY, TIME }; - DateGran granularity = null; - /** - * DateFormatSymbols for locale monthsname + * DateFormatSymbols for locale months name */ private static DateFormatSymbols dfs = null; - + /** * note the session locale */ private static Locale langMonth = null; - - - /** - * Construct a clean date - */ - public DCDate() - { - super(); - } + /** * Construct a date object from a Java Date object. @@ -147,168 +137,26 @@ public class DCDate */ public DCDate(Date date) { - super(); - if(date != null) - { - setTime(date); - if (!(calendar.get(Calendar.HOUR_OF_DAY) == 0 && - calendar.get(Calendar.MINUTE) == 0 && - calendar.get(Calendar.SECOND) == 0)) - { - granularity = DateGran.TIME; - } - // if date is 1-jan, assume it's because this was set for year - else if (calendar.get(Calendar.DAY_OF_MONTH) == 1 && calendar.get(Calendar.MONTH) == 0) - { - granularity = DateGran.YEAR; - } - // otherwise day - else - { - granularity = DateGran.DAY; - } - } - } - - /** - * Construct a date from a Dublin Core value - * - * @param fromDC - * the date string, in ISO 8601 (no timezone, always use UTC/GMT) - */ - public DCDate(String fromDC) - { - super(); - - // An empty date is OK - if ((fromDC == null) || fromDC.equals("")) + if (date == null) { return; } - // default granularity + + // By definition a Date has a time component so always set the granularity to TIME. granularity = DateGran.TIME; - Date date = tryParse(fullIso, fromDC); - if (date == null) - date = tryParse(fullIso2, fromDC); - if (date == null) - date = tryParse(fullIso3, fromDC); - if (date == null) - { - // NOTE: move GMT date to local midnight when granularity is coarse - date = tryParse(dateIso, fromDC); - if (date != null) - { - long ldate = date.getTime(); - date = new Date(ldate - localZone.getOffset(ldate)); - granularity = DateGran.DAY; - } - } - if (date == null) - { - // NOTE: move GMT date to local midnight when granularity is coarse - date = tryParse(yearMonthIso, fromDC); - if (date != null) - { - long ldate = date.getTime(); - date = new Date(ldate - localZone.getOffset(ldate)); - granularity = DateGran.MONTH; - } - } - if (date == null) - { - // NOTE: move GMT date to local midnight when granularity is coarse - date = tryParse(yearIso, fromDC); - if (date != null) - { - long ldate = date.getTime(); - date = new Date(ldate - localZone.getOffset(ldate)); - granularity = DateGran.YEAR; - } - } - if (date == null) - log.warn("Mangled date: " + fromDC + " ..failed all attempts to parse as date."); - else - setTime(date); - } - - // Attempt to parse, swallowing errors; return null for failure. - private synchronized Date tryParse(SimpleDateFormat sdf, String source) - { - try - { - return sdf.parse(source); - } - catch (ParseException pe) - { - return null; - } - } - - /** - * Set the time components to reflect the absolute time in this Date. - * - * @param date - * the Java Date object. - */ - private void setTime(Date date) - { + // Set the local calendar. + localCalendar = new GregorianCalendar(); + localCalendar.setTime(date); + + // Now set the UTC equivalent. calendar = new GregorianCalendar(utcZone); calendar.setTime(date); - } + } /** - * Get a date representing the current instant in time. - * - * @return a DSpaceDate object representing the current instant. - */ - public static DCDate getCurrent() - { - return (new DCDate(new Date())); - } - - /** - * Get the date as a string to put back in the Dublin Core - * - * @return The date as a string. - */ - public String toString() - { - if (calendar == null) - return "null"; - return toStringInternal(); - } - - // When granularity is "day" or more, show the _local-time_ day because - // when the granularity was coarse the local time value was set. - private synchronized String toStringInternal() - { - if (granularity == DateGran.YEAR) - return String.format("%4d", getYear()); - else if (granularity == DateGran.MONTH) - return String.format("%4d-%02d", getYear(), getMonth()); - else if (granularity == DateGran.DAY) - return String.format("%4d-%02d-%02d", getYear(), getMonth(), getDay()); - else - return fullIso.format(calendar.getTime()); - } - - /** - * Get the date as a Java Date object, assuming the granularity is sufficient for it to be expressed as a Date. - * - * @return a Date object - */ - public Date toDate() - { - if ((calendar == null) || (!withinGranularity(DateGran.DAY))) - return null; - else - return calendar.getTime(); - } - /** - * Set the date. The date passed in is assumed to be in the current time - * zone, and is adjusting to fit the current time zone. Unknown values - * should be given as -1. + * Construct a date object from a bunch of component parts. The date passed in is assumed to be in the current + * time zone. Unknown values should be given as -1. * * @param yyyy * the year @@ -323,8 +171,7 @@ public class DCDate * @param ss * the seconds */ - - public void setDateLocal(int yyyy, int mm, int dd, int hh, int mn, int ss) + public DCDate(int yyyy, int mm, int dd, int hh, int mn, int ss) { // default values int lyear = 0; @@ -365,25 +212,109 @@ public class DCDate granularity = DateGran.TIME; } - // do the timezone adjustment: get Date and put it in UTC zone. - GregorianCalendar localGC = new GregorianCalendar(lyear, lmonth - 1, lday, - lhours, lminutes, lseconds); - setTime(localGC.getTime()); - } + // Set the local calendar. + localCalendar = new GregorianCalendar(lyear, lmonth - 1, lday, + lhours, lminutes, lseconds); - // get cached calendar in local timezone - private GregorianCalendar getLocalCalendar() - { - if (localCalendar == null) + if (granularity == DateGran.TIME) { - if (calendar == null) - return null; - localCalendar = new GregorianCalendar(); - localCalendar.setTime(calendar.getTime()); + // Now set the UTC equivalent. + calendar = new GregorianCalendar(utcZone); + calendar.setTime(localCalendar.getTime()); + } + else + { + // No Time component so just set the UTC date to be the same as the local Year, Month, and Day. + calendar = new GregorianCalendar(localCalendar.get(Calendar.YEAR), localCalendar.get(Calendar.MONTH), localCalendar.get(Calendar.DAY_OF_MONTH)); } - return localCalendar; } + /** + * Construct a date from a Dublin Core value + * + * @param fromDC + * the date string, in ISO 8601 (no timezone, always use UTC) + */ + public DCDate(String fromDC) + { + // An empty date is OK + if ((fromDC == null) || fromDC.equals("")) + { + return; + } + + // default granularity + granularity = DateGran.TIME; + Date date = tryParse(fullIso, fromDC); + if (date == null) + date = tryParse(fullIso2, fromDC); + if (date == null) + date = tryParse(fullIso3, fromDC); + if (date == null) + date = tryParse(fullIso4, fromDC); + if (date == null) + { + // Seems there is no time component to the date. + date = tryParse(dateIso, fromDC); + if (date != null) + { + granularity = DateGran.DAY; + } + } + if (date == null) + { + date = tryParse(yearMonthIso, fromDC); + if (date != null) + { + granularity = DateGran.MONTH; + } + } + if (date == null) + { + date = tryParse(yearIso, fromDC); + if (date != null) + { + granularity = DateGran.YEAR; + } + } + + if (date == null) + { + log.warn("Mangled date: " + fromDC + " ..failed all attempts to parse as date."); + } + else + { + // Set the UTC time. + calendar = new GregorianCalendar(utcZone); + calendar.setTime(date); + + // Now set the local equivalent. + if (granularity == DateGran.TIME) + { + localCalendar = new GregorianCalendar(); + localCalendar.setTime(date); + } + else + { + // No Time component so just set the local date to be the same as the UTC Year, Month, and Day. + localCalendar = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); + } + } + } + + // Attempt to parse, swallowing errors; return null for failure. + private synchronized Date tryParse(SimpleDateFormat sdf, String source) + { + try + { + return sdf.parse(source); + } + catch (ParseException pe) + { + return null; + } + } + /** * Get the year, adjusting for current time zone. * @@ -391,9 +322,9 @@ public class DCDate */ public int getYear() { - return ((getLocalCalendar() == null) || (!withinGranularity(DateGran.YEAR))) ? -1 : localCalendar.get(Calendar.YEAR); + return (!withinGranularity(DateGran.YEAR)) ? -1 : localCalendar.get(Calendar.YEAR); } - + /** * Get the month, adjusting for current time zone. * @@ -401,7 +332,7 @@ public class DCDate */ public int getMonth() { - return ((getLocalCalendar() == null) || (!withinGranularity(DateGran.MONTH))) ? -1 : localCalendar.get(Calendar.MONTH) + 1; + return (!withinGranularity(DateGran.MONTH)) ? -1 : localCalendar.get(Calendar.MONTH) + 1; } /** @@ -411,7 +342,7 @@ public class DCDate */ public int getDay() { - return ((getLocalCalendar() == null) || (!withinGranularity(DateGran.DAY))) ? -1 : localCalendar.get(Calendar.DAY_OF_MONTH); + return (!withinGranularity(DateGran.DAY)) ? -1 : localCalendar.get(Calendar.DAY_OF_MONTH); } /** @@ -421,7 +352,7 @@ public class DCDate */ public int getHour() { - return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.HOUR_OF_DAY); + return (!withinGranularity(DateGran.TIME)) ? -1 : localCalendar.get(Calendar.HOUR_OF_DAY); } /** @@ -431,7 +362,7 @@ public class DCDate */ public int getMinute() { - return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.MINUTE); + return (!withinGranularity(DateGran.TIME)) ? -1 : localCalendar.get(Calendar.MINUTE); } /** @@ -441,77 +372,254 @@ public class DCDate */ public int getSecond() { - return getLocalCalendar() == null ? -1 : localCalendar.get(Calendar.SECOND); + return (!withinGranularity(DateGran.TIME)) ? -1 : localCalendar.get(Calendar.SECOND); } /** - * Get the year in GMT. + * Get the year in UTC. * * @return the year */ - public int getYearGMT() + public int getYearUTC() { - return calendar == null ? -1 : calendar.get(Calendar.YEAR); + return (!withinGranularity(DateGran.YEAR)) ? -1 : calendar.get(Calendar.YEAR); } /** - * Get the month in GMT. + * Get the month in UTC. * * @return the month */ - public int getMonthGMT() + public int getMonthUTC() { - return calendar == null ? -1 : calendar.get(Calendar.MONTH) + 1; + return (!withinGranularity(DateGran.MONTH)) ? -1 : calendar.get(Calendar.MONTH) + 1; } /** - * Get the day in GMT. + * Get the day in UTC. * * @return the day */ - public int getDayGMT() + public int getDayUTC() { - return calendar == null ? -1 : calendar.get(Calendar.DAY_OF_MONTH); + return (!withinGranularity(DateGran.DAY)) ? -1 : calendar.get(Calendar.DAY_OF_MONTH); } /** - * Get the hour in GMT. + * Get the hour in UTC. * * @return the hour */ - public int getHourGMT() + public int getHourUTC() { - return calendar == null ? -1 : calendar.get(Calendar.HOUR_OF_DAY); + return (!withinGranularity(DateGran.TIME)) ? -1 : calendar.get(Calendar.HOUR_OF_DAY); } /** - * Get the minute in GMT. + * Get the minute in UTC. * * @return the minute */ - public int getMinuteGMT() + public int getMinuteUTC() { - return calendar == null ? -1 : calendar.get(Calendar.MINUTE); + return (!withinGranularity(DateGran.TIME)) ? -1 : calendar.get(Calendar.MINUTE); } /** - * Get the second in GMT. + * Get the second in UTC. * * @return the second */ - public int getSecondGMT() + public int getSecondUTC() { - return calendar == null ? -1 : calendar.get(Calendar.SECOND); + return (!withinGranularity(DateGran.TIME)) ? -1 : calendar.get(Calendar.SECOND); + } + + + /** + * Get the date as a string to put back in the Dublin Core. Use the UTC/GMT calendar version. + * + * @return The date as a string. + */ + public String toString() + { + if (calendar == null) + return "null"; + return toStringInternal(); + } + + private synchronized String toStringInternal() + { + if (granularity == DateGran.YEAR) + return String.format("%4d", getYearUTC()); + else if (granularity == DateGran.MONTH) + return String.format("%4d-%02d", getYearUTC(), getMonthUTC()); + else if (granularity == DateGran.DAY) + return String.format("%4d-%02d-%02d", getYearUTC(), getMonthUTC(), getDayUTC()); + else + return fullIso.format(calendar.getTime()); } /** + * Get the date as a Java Date object. + * + * @return a Date object + */ + public Date toDate() + { + if (calendar == null) + return null; + else + return calendar.getTime(); + } + + /** + * 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 UTC + * @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 (isLocalTime) + { + return displayLocalDate(showTime, locale); + } + else + { + return displayUTCDate(showTime, locale); + } + } + + public String displayLocalDate(boolean showTime, Locale locale) + { + // forcibly truncate month name to 3 chars -- XXX FIXME? + String monthName = getMonthName(getMonth(), locale).substring(0, 3); + + // display date and time + if (showTime && granularity == DateGran.TIME) + { + return String.format("%d-%s-%4d %02d:%02d:%02d", getDay(), monthName, getYear(), getHour(), getMinute(), getSecond()); + } + else if (granularity == DateGran.YEAR) + { + return String.format("%4d", getYear()); + } + else if (granularity == DateGran.MONTH) + { + return String.format("%s-%4d", monthName, getYear()); + } + else + { + return String.format("%d-%s-%4d", getDay(), monthName, getYear()); + } + } + + public String displayUTCDate(boolean showTime, Locale locale) + { + // forcibly truncate month name to 3 chars -- XXX FIXME? + String monthName = getMonthName(getMonthUTC(), locale).substring(0, 3); + + // display date and time + if (showTime && granularity == DateGran.TIME) + { + return String.format("%d-%s-%4d %02d:%02d:%02d", getDayUTC(), monthName, getYearUTC(), getHourUTC(), getMinuteUTC(), getSecondUTC()); + } + else if (granularity == DateGran.YEAR) + { + return String.format("%4d", getYearUTC()); + } + else if (granularity == DateGran.MONTH) + { + return String.format("%s-%4d", monthName, getYearUTC()); + } + else + { + return String.format("%d-%s-%4d", getDayUTC(), monthName, getYearUTC()); + } + } + + /** + * Test if the requested level of granularity is within that of the date. + * + * @param dg + * The requested level of granularity. + * @return + * true or false. + * + */ + private boolean withinGranularity(DateGran dg) + { + if (granularity == DateGran.TIME) + { + if ((dg == DateGran.TIME) || (dg == DateGran.DAY) || (dg == DateGran.MONTH) || (dg == DateGran.YEAR)) + { + return true; + } + } + + if (granularity == DateGran.DAY) + { + if ((dg == DateGran.DAY) || (dg == DateGran.MONTH) || (dg == DateGran.YEAR)) + { + return true; + } + } + + if (granularity == DateGran.MONTH) + { + if ((dg == DateGran.MONTH) || (dg == DateGran.YEAR)) + { + return true; + } + } + + if (granularity == DateGran.YEAR) + { + if (dg == DateGran.YEAR) + { + return true; + } + } + + return false; + } + + + + /************** Some utility methods ******************/ + + /** + * Get a date representing the current instant in time. + * + * @return a DSpaceDate object representing the current instant. + + */ + public static DCDate getCurrent() + { + return (new DCDate(new Date())); + } + + /** * Get a month's name for a month between 1 and 12. Any invalid month value * (e.g. 0 or -1) will return a value of "Unspecified". * * @param m * the month number * - * @return the month name. + * @param locale + * + * @return the month name. */ public static String getMonthName(int m, Locale locale) { @@ -531,178 +639,4 @@ public class DCDate } - /** - * Test if the requested level of granularity is within that of the date. - * - * @param dg - * The requested level of granularity. - * @return - * true or false. - * - */ - private boolean withinGranularity(DateGran dg) - { - if (granularity == DateGran.TIME) - { - if ((dg == DateGran.TIME) || (dg == DateGran.DAY) || (dg == DateGran.MONTH) || (dg == DateGran.YEAR)) - { - return true; - } - } - - if (granularity == DateGran.DAY) - { - if ((dg == DateGran.DAY) || (dg == DateGran.MONTH) || (dg == DateGran.YEAR)) - { - return true; - } - } - - if (granularity == DateGran.MONTH) - { - if ((dg == DateGran.MONTH) || (dg == DateGran.YEAR)) - { - return true; - } - } - - if (granularity == DateGran.YEAR) - { - if (dg == DateGran.YEAR) - { - return true; - } - } - - return false; - } - - /** - * Simple test program - * Usage: java org.dspace.content.DCdate [DCDate | -l yyyy [mm [dd ..]]] ] - * where "DCDate" is the kind of value that would be in metadata, - * e.g. "2006", "2006-02-03", etc. - * (-l form tests local time parsing) - * Default is to use current time. - */ - public static void main(String args[]) - throws Exception - { - DCDate d; - - // if there's an arg, parse it for the date, otherwise use now - if (args.length > 0) - { - if (args[0].equals("-l")) - { - int val[] = { -1, -1, -1, -1, -1, -1 }; - for (int i = 1; i < 7 && i < args.length; ++i) - val[i-1] = Integer.parseInt(args[i]); - d = new DCDate(); - d.setDateLocal(val[0], val[1], val[2], val[3], val[4], val[5]); - } - else - d = new DCDate(args[0]); - } - else - d = DCDate.getCurrent(); - - // 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); - System.out.println("getYear(), = "+d.getYear()); - System.out.println("getMonth(), = "+d.getMonth()); - System.out.println("getDay(), = "+d.getDay()); - System.out.println("getHour(), = "+d.getHour()); - System.out.println("getMinute(), = "+d.getMinute()); - System.out.println("getSecond()); = "+d.getSecond()); - System.out.println("By GMT component:"); - System.out.println("getYearGMT(), = "+d.getYearGMT()); - System.out.println("getMonthGMT(), = "+d.getMonthGMT()); - System.out.println("getDayGMT(), = "+d.getDayGMT()); - System.out.println("getHourGMT(), = "+d.getHourGMT()); - System.out.println("getMinuteGMT(), = "+d.getMinuteGMT()); - System.out.println("getSecondGMT()); = "+d.getSecondGMT()); - - // convert it the hard way: - DCDate hw = new DCDate(); - hw.setDateLocal(d.getYear(),d.getMonth(),d.getDay(), - d.getHour(),d.getMinute(),d.getSecond()); - System.out.println("hardway.toString() = \""+hw.toString()+"\""); - - // 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.YEAR) - { - return String.format("%4d", dd.getYear()); - } - else if (granularity == DateGran.MONTH) - { - return String.format("%s-%4d", monthName, dd.getYear()); - } - else - { - return String.format("%d-%s-%4d", dd.getDay(), monthName, dd.getYear()); - } - } -} +} \ No newline at end of file diff --git a/dspace-api/src/main/java/org/dspace/content/InstallItem.java b/dspace-api/src/main/java/org/dspace/content/InstallItem.java index 274b005333..89dd08b5cb 100644 --- a/dspace-api/src/main/java/org/dspace/content/InstallItem.java +++ b/dspace-api/src/main/java/org/dspace/content/InstallItem.java @@ -195,8 +195,7 @@ public class InstallItem if (currentDateIssued.length == 0) { - DCDate issued = new DCDate(); - issued.setDateLocal(now.getYear(),now.getMonth(),now.getDay(),-1,-1,-1); + DCDate issued = new DCDate(now.getYear(),now.getMonth(),now.getDay(),-1,-1,-1); item.addDC("date", "issued", null, issued.toString()); } diff --git a/dspace-api/src/main/java/org/dspace/submit/step/DescribeStep.java b/dspace-api/src/main/java/org/dspace/submit/step/DescribeStep.java index 570897c8c9..e372b51ec1 100644 --- a/dspace-api/src/main/java/org/dspace/submit/step/DescribeStep.java +++ b/dspace-api/src/main/java/org/dspace/submit/step/DescribeStep.java @@ -780,9 +780,7 @@ public class DescribeStep extends AbstractProcessingStep // FIXME: Probably should be some more validation // Make a standard format date - DCDate d = new DCDate(); - - d.setDateLocal(year, month, day, -1, -1, -1); + DCDate d = new DCDate(year, month, day, -1, -1, -1); // already done in doProcessing see also bug DS-203 // item.clearMetadata(schema, element, qualifier, Item.ANY); diff --git a/dspace-test/src/test/java/org/dspace/content/DCDateTest.java b/dspace-test/src/test/java/org/dspace/content/DCDateTest.java index ac05ea7e18..361bd36f15 100644 --- a/dspace-test/src/test/java/org/dspace/content/DCDateTest.java +++ b/dspace-test/src/test/java/org/dspace/content/DCDateTest.java @@ -34,12 +34,8 @@ package org.dspace.content; import org.apache.log4j.Logger; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.List; -import java.util.Locale; + +import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -84,8 +80,7 @@ public class DCDateTest extends AbstractUnitTest public void init() { super.init(); - dc = new DCDate(); - c = new GregorianCalendar(); + TimeZone.setDefault(TimeZone.getTimeZone("GMT-8")); } /** @@ -108,42 +103,97 @@ public class DCDateTest extends AbstractUnitTest * Test of DCDate constructor, of class DCDate. */ @Test - public void testDCDate() + public void testDCDateDate() { - dc = new DCDate(); - assertThat("testDCDate 0", dc.toString(), equalTo("null")); + dc = new DCDate((String)null); + assertThat("testDCDateDate 1", dc.getYear(), equalTo(-1)); + assertThat("testDCDateDate 2", dc.getMonth(), equalTo(-1)); + assertThat("testDCDateDate 3", dc.getDay(), equalTo(-1)); + assertThat("testDCDateDate 4", dc.getHour(), equalTo(-1)); + assertThat("testDCDateDate 5", dc.getMinute(), equalTo(-1)); + assertThat("testDCDateDate 6", dc.getSecond(), equalTo(-1)); + + assertThat("testDCDateDate 7", dc.getYearUTC(), equalTo(-1)); + assertThat("testDCDateDate 8", dc.getMonthUTC(), equalTo(-1)); + assertThat("testDCDateDate 9", dc.getDayUTC(), equalTo(-1)); + assertThat("testDCDateDate 10", dc.getHourUTC(), equalTo(-1)); + assertThat("testDCDateDate 11", dc.getMinuteUTC(), equalTo(-1)); + assertThat("testDCDateDate 12", dc.getSecondUTC(), equalTo(-1)); + + // NB. Months begin at 0 in GregorianCalendar so 0 is January. + c = new GregorianCalendar(2010,0,1); + dc = new DCDate(c.getTime()); + + assertThat("testDCDateDate 1 ", dc.getYear(), equalTo(2010)); + assertThat("testDCDateDate 2 ", dc.getMonth(), equalTo(1)); + assertThat("testDCDateDate 3 ", dc.getDay(), equalTo(1)); + assertThat("testDCDateDate 4 ", dc.getHour(), equalTo(0)); + assertThat("testDCDateDate 5 ", dc.getMinute(), equalTo(0)); + assertThat("testDCDateDate 6 ", dc.getSecond(), equalTo(0)); + + assertThat("testDCDateDate 7 ", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateDate 8 ", dc.getMonthUTC(), equalTo(1)); + assertThat("testDCDateDate 9 ", dc.getDayUTC(), equalTo(1)); + assertThat("testDCDateDate 10 ", dc.getHourUTC(), equalTo(8)); + assertThat("testDCDateDate 11 ", dc.getMinuteUTC(), equalTo(0)); + assertThat("testDCDateDate 12 ", dc.getSecondUTC(), equalTo(0)); + + c = new GregorianCalendar(2009,11,31,18,30); + dc = new DCDate(c.getTime()); + + assertThat("testDCDateDate 13 ", dc.getYear(), equalTo(2009)); + assertThat("testDCDateDate 14 ", dc.getMonth(), equalTo(12)); + assertThat("testDCDateDate 15 ", dc.getDay(), equalTo(31)); + assertThat("testDCDateDate 16 ", dc.getHour(), equalTo(18)); + assertThat("testDCDateDate 17 ", dc.getMinute(), equalTo(30)); + assertThat("testDCDateDate 18 ", dc.getSecondUTC(), equalTo(0)); + + assertThat("testDCDateDate 19 ", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateDate 20 ", dc.getMonthUTC(), equalTo(1)); + assertThat("testDCDateDate 21 ", dc.getDayUTC(), equalTo(1)); + assertThat("testDCDateDate 22 ", dc.getHourUTC(), equalTo(2)); + assertThat("testDCDateDate 23 ", dc.getMinuteUTC(), equalTo(30)); + assertThat("testDCDateDate 24 ", dc.getSecondUTC(), equalTo(0)); } /** * Test of DCDate constructor, of class DCDate. */ @Test - public void testDCDateDate() + public void testDCDateIntBits() { - dc = new DCDate((Date)null); - assertThat("testDCDateDate 0", dc.toString(), equalTo("null")); + dc = new DCDate(2010,1,1,-1,-1,-1); - // If date is Jan 1st, DCDate incorrectly treats this as year granularity - // c = new GregorianCalendar(2010,0,1); - c = new GregorianCalendar(2010,1,1); - dc = new DCDate(c.getTime()); - assertThat("testDCDateDate 1", dc.toString(), equalTo("2010-02-01")); + assertThat("testDCDateIntBits 1", dc.getYear(), equalTo(2010)); + assertThat("testDCDateIntBits 2", dc.getMonth(), equalTo(1)); + assertThat("testDCDateIntBits 3", dc.getDay(), equalTo(1)); + assertThat("testDCDateIntBits 4", dc.getHour(), equalTo(-1)); + assertThat("testDCDateIntBits 5", dc.getMinute(), equalTo(-1)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(-1)); - // DCDate doesn't currently support month granularity when constructed from a calendar object - // c = new GregorianCalendar(2010,3,0); - // dc = new DCDate(c.getTime()); - // assertThat("testDCDateDate 2", dc.toString(), equalTo("2010-04")); + assertThat("testDCDateIntBits 7", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateIntBits 8", dc.getMonthUTC(), equalTo(1)); + assertThat("testDCDateIntBits 9", dc.getDayUTC(), equalTo(1)); + assertThat("testDCDateIntBits 10", dc.getHourUTC(), equalTo(-1)); + assertThat("testDCDateIntBits 11", dc.getMinuteUTC(), equalTo(-1)); + assertThat("testDCDateIntBits 12", dc.getSecondUTC(), equalTo(-1)); - // Broken by a 1 hour offset - // c = new GregorianCalendar(2010,3,14); - // dc = new DCDate(c.getTime()); - // assertThat("testDCDateDate 3", dc.toString(), equalTo("2010-04-14")); + dc = new DCDate(2009,12,31,18,30,5); + + assertThat("testDCDateIntBits 13", dc.getYear(), equalTo(2009)); + assertThat("testDCDateIntBits 14", dc.getMonth(), equalTo(12)); + assertThat("testDCDateIntBits 15", dc.getDay(), equalTo(31)); + assertThat("testDCDateIntBits 16", dc.getHour(), equalTo(18)); + assertThat("testDCDateIntBits 17", dc.getMinute(), equalTo(30)); + assertThat("testDCDateIntBits 18", dc.getSecond(), equalTo(5)); + + assertThat("testDCDateIntBits 19", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateIntBits 20", dc.getMonthUTC(), equalTo(1)); + assertThat("testDCDateIntBits 21", dc.getDayUTC(), equalTo(1)); + assertThat("testDCDateIntBits 22", dc.getHourUTC(), equalTo(2)); + assertThat("testDCDateIntBits 23", dc.getMinuteUTC(), equalTo(30)); + assertThat("testDCDateIntBits 24", dc.getSecondUTC(), equalTo(5)); - // Broken by a 1 hour offset - // c = new GregorianCalendar(2010,3,14,0,0,1); - // dc = new DCDate(c.getTime()); - // assertThat("testDCDateDate 4", dc.toString(), - // equalTo("2010-04-14T00:00:01Z")); } /** @@ -153,42 +203,127 @@ public class DCDateTest extends AbstractUnitTest public void testDCDateString() { dc = new DCDate((String)null); - assertThat("testDCDateString 0", dc.toString(), equalTo("null")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(-1)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(-1)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(-1)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(-1)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(-1)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(-1)); + + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(-1)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(-1)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(-1)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(-1)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(-1)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(-1)); dc = new DCDate(""); - assertThat("testDCDateString 1", dc.toString(), equalTo("null")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(-1)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(-1)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(-1)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(-1)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(-1)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(-1)); + + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(-1)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(-1)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(-1)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(-1)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(-1)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(-1)); dc = new DCDate("2010"); - assertThat("testDCDateString 2", dc.toString(), equalTo("2010")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(2010)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(-1)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(-1)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(-1)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(-1)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(-1)); + + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(-1)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(-1)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(-1)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(-1)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(-1)); dc = new DCDate("2010-04"); - assertThat("testDCDateString 3", dc.toString(), equalTo("2010-04")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(2010)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(04)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(-1)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(-1)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(-1)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(-1)); + + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(04)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(-1)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(-1)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(-1)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(-1)); dc = new DCDate("2010-04-14"); - assertThat("testDCDateString 4", dc.toString(), equalTo("2010-04-14")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(2010)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(04)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(14)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(-1)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(-1)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(-1)); + + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(04)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(14)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(-1)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(-1)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(-1)); dc = new DCDate("2010-04-14T01"); - assertThat("testDCDateString 5", dc.toString(), equalTo("2010-04-14")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(2010)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(04)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(13)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(17)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(0)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(0)); + + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(04)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(14)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(1)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(0)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(0)); dc = new DCDate("2010-04-14T00:01"); - assertThat("testDCDateString 6", dc.toString(), - equalTo("2010-04-14T00:01:00Z")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(2010)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(04)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(13)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(16)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(1)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(0)); + + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(04)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(14)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(0)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(1)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(0)); dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testDCDateString 7", dc.toString(), - equalTo("2010-04-14T00:00:01Z")); + assertThat("testDCDateString 1", dc.getYear(), equalTo(2010)); + assertThat("testDCDateString 2", dc.getMonth(), equalTo(04)); + assertThat("testDCDateString 3", dc.getDay(), equalTo(13)); + assertThat("testDCDateString 4", dc.getHour(), equalTo(16)); + assertThat("testDCDateString 5", dc.getMinute(), equalTo(0)); + assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(1)); + assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(2010)); + assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(04)); + assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(14)); + assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(0)); + assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(0)); + assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(1)); } - /** - * Test of getCurrent method, of class DCDate. - */ - @Test - public void testGetCurrent() - { - assertThat("testGetCurrent 0", DCDate.getCurrent().toDate(), - equalTo(new Date())); - } + /** * Test of toString method, of class DCDate. @@ -212,7 +347,7 @@ public class DCDateTest extends AbstractUnitTest assertThat("testToString 4", dc.toString(), equalTo("2010-04-14")); dc = new DCDate("2010-04-14T01"); - assertThat("testToString 5", dc.toString(), equalTo("2010-04-14")); + assertThat("testToString 5", dc.toString(), equalTo("2010-04-14T01:00:00Z")); dc = new DCDate("2010-04-14T00:01"); assertThat("testToString 6", dc.toString(), @@ -249,344 +384,62 @@ public class DCDateTest extends AbstractUnitTest assertThat("testToDate 4", dc.toDate(), equalTo(c.getTime())); } - /** - * Test of setDateLocal method, of class DCDate. - */ - @Test - public void testSetDateLocal() - { - dc = new DCDate(""); - dc.setDateLocal(2010,0,0,-1,-1,-1); - assertThat("testSetDateLocal 0", dc.toString(), equalTo("2010")); - dc = new DCDate(""); - dc.setDateLocal(2010,4,0,-1,-1,-1); - assertThat("testSetDateLocal 1", dc.toString(), equalTo("2010-04")); - dc = new DCDate(""); - dc.setDateLocal(2010,4,14,-1,-1,-1); - assertThat("testSetDateLocal 2", dc.toString(), equalTo("2010-04-14")); - - // Broken by a 1 hour offset - // dc = new DCDate(""); - // dc.setDateLocal(2010,4,14,5,5,5); - // assertThat("testSetDateLocal 3", dc.toString(), - // equalTo("2010-04-14T05:05:05Z")); - - // Broken by a 1 hour offset - // dc = new DCDate(""); - // dc.setDateLocal(2010,4,14,0,0,1); - // assertThat("testSetDateLocal 4", dc.toString(), - // equalTo("2010-04-14T00:00:01Z")); - } /** - * Test of getYear method, of class DCDate. + * Test of displayDate method, of class DCDate. */ @Test - public void testGetYear() + public void testDisplayDate() { - dc = new DCDate((String)null); - assertThat("testGetYear 0", dc.getYear(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetYear 1", dc.getYear(), equalTo(-1)); - dc = new DCDate("2010"); - assertThat("testGetYear 2", dc.getYear(), equalTo(2010)); + assertThat("testDisplayDate 1 ", dc.displayDate(true, true, + new Locale("en_GB")), + equalTo("2010")); dc = new DCDate("2010-04"); - assertThat("testGetYear 3", dc.getYear(), equalTo(2010)); + assertThat("testDisplayDate 2 ", dc.displayDate(true, true, + new Locale("en_GB")), + equalTo("Apr-2010")); dc = new DCDate("2010-04-14"); - assertThat("testGetYear 4", dc.getYear(), equalTo(2010)); + assertThat("testDisplayDate 3 ", dc.displayDate(true, true, + new Locale("en_GB")), + equalTo("14-Apr-2010")); dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testGetYear 5", dc.getYear(), equalTo(2010)); - } - - /** - * Test of getMonth method, of class DCDate. - */ - @Test - public void testGetMonth() - { - dc = new DCDate((String)null); - assertThat("testGetMonth 0", dc.getMonth(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetMonth 1", dc.getMonth(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetMonth 2", dc.getMonth(), equalTo(-1)); - - dc = new DCDate("2010-04"); - assertThat("testGetMonth 3", dc.getMonth(), equalTo(4)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetMonth 4", dc.getMonth(), equalTo(4)); + assertThat("testDisplayDate 4 ", dc.displayDate(true, true, + new Locale("en_GB")), + equalTo("13-Apr-2010 16:00:01")); dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testGetMonth 5", dc.getMonth(), equalTo(4)); - } - - /** - * Test of getDay method, of class DCDate. - */ - @Test - public void testGetDay() - { - dc = new DCDate((String)null); - assertThat("testGetDay 0", dc.getDay(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetDay 1", dc.getDay(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetDay 2", dc.getDay(), equalTo(-1)); - - dc = new DCDate("2010-04"); - assertThat("testGetDay 3", dc.getDay(), equalTo(-1)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetDay 4", dc.getDay(), equalTo(14)); + assertThat("testDisplayDate 5 ", dc.displayDate(false, true, + new Locale("en_GB")), + equalTo("13-Apr-2010")); dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testGetDay 5", dc.getDay(), equalTo(14)); - } - - /** - * Test of getHour method, of class DCDate. - */ - @Test - public void testGetHour() - { - dc = new DCDate((String)null); - assertThat("testGetHour 0", dc.getHour(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetHour 1", dc.getHour(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetHour 2", dc.getHour(), equalTo(0)); - - dc = new DCDate("2010-04"); - assertThat("testGetHour 3", dc.getHour(), equalTo(0)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetHour 4", dc.getHour(), equalTo(0)); - - // Broken with 1 hour offset - // dc = new DCDate("2010-04-14T01:00:00Z"); - // assertThat("testGetHour 5", dc.getHour(), equalTo(1)); - } - - /** - * Test of getMinute method, of class DCDate. - */ - @Test - public void testGetMinute() - { - dc = new DCDate((String)null); - assertThat("testGetMinute 0", dc.getMinute(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetMinute 1", dc.getMinute(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetMinute 2", dc.getMinute(), equalTo(0)); - - dc = new DCDate("2010-04"); - assertThat("testGetMinute 3", dc.getMinute(), equalTo(0)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetMinute 4", dc.getMinute(), equalTo(0)); - - dc = new DCDate("2010-04-14T00:01:00Z"); - assertThat("testGetMinute 5", dc.getMinute(), equalTo(1)); - } - - /** - * Test of getSecond method, of class DCDate. - */ - @Test - public void testGetSecond() - { - dc = new DCDate((String)null); - assertThat("testGetSecond 0", dc.getSecond(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetSecond 1", dc.getSecond(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetSecond 2", dc.getSecond(), equalTo(0)); - - dc = new DCDate("2010-04"); - assertThat("testGetSecond 3", dc.getSecond(), equalTo(0)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetSecond 4", dc.getSecond(), equalTo(0)); + assertThat("testDisplayDate 6 ", dc.displayDate(true, false, + new Locale("es")), + equalTo("14-abr-2010 00:00:01")); dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testGetSecond 5", dc.getSecond(), equalTo(1)); + assertThat("testDisplayDate 7 ", dc.displayDate(false, false, + new Locale("en_GB")), + equalTo("14-Apr-2010")); } /** - * Test of getYearGMT method, of class DCDate. + * Test of getCurrent method, of class DCDate. */ @Test - public void testGetYearGMT() + public void testGetCurrent() { - dc = new DCDate((String)null); - assertThat("testGetYearGMT 0", dc.getYearGMT(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetYearGMT 1", dc.getYearGMT(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetYearGMT 2", dc.getYearGMT(), equalTo(2010)); - - dc = new DCDate("2010-04"); - assertThat("testGetYearGMT 3", dc.getYearGMT(), equalTo(2010)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetYearGMT 4", dc.getYearGMT(), equalTo(2010)); - - dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testGetYearGMT 5", dc.getYearGMT(), equalTo(2010)); + assertThat("testGetCurrent 0", DCDate.getCurrent().toDate(), + equalTo(new Date())); } - /** - * Test of getMonthGMT method, of class DCDate. - */ - @Test - public void testGetMonthGMT() - { - dc = new DCDate((String)null); - assertThat("testGetMonthGMT 0", dc.getMonthGMT(), equalTo(-1)); - dc = new DCDate(""); - assertThat("testGetMonthGMT 1", dc.getMonthGMT(), equalTo(-1)); - - // Should return 0, returns 1 - // dc = new DCDate("2010"); - //assertThat("testGetMonthGMT 2", dc.getMonthGMT(), equalTo(0)); - - dc = new DCDate("2010-04"); - assertThat("testGetMonthGMT 3", dc.getMonthGMT(), equalTo(3)); - - // Should return 3, returns 4 - // dc = new DCDate("2010-04-14"); - // assertThat("testGetMonthGMT 4", dc.getMonthGMT(), equalTo(3)); - - // Should return 3, returns 4 - // dc = new DCDate("2010-04-14T00:00:01Z"); - // assertThat("testGetMonthGMT 5", dc.getMonthGMT(), equalTo(3)); - } - - /** - * Test of getDayGMT method, of class DCDate. - */ - @Test - public void testGetDayGMT() - { - dc = new DCDate((String)null); - assertThat("testGetDayGMT 0", dc.getDayGMT(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetDayGMT 1", dc.getDayGMT(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetDayGMT 2", dc.getDayGMT(), equalTo(1)); - - // Expect 1, gets 31 - //dc = new DCDate("2010-04"); - //assertThat("testGetDayGMT 3", dc.getDayGMT(), equalTo(1)); - - // Another day less than expected, gets 13 instead of 14 - // dc = new DCDate("2010-04-14"); - //assertThat("testGetDayGMT 4", dc.getDayGMT(), equalTo(14)); - - dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testGetDayGMT 5", dc.getDayGMT(), equalTo(14)); - } - - /** - * Test of getHourGMT method, of class DCDate. - */ - @Test - public void testGetHourGMT() - { - dc = new DCDate((String)null); - assertThat("testGetHourGMT 0", dc.getHourGMT(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetHourGMT 1", dc.getHourGMT(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetHourGMT 2", dc.getHourGMT(), equalTo(0)); - - // One hour out, returns 23 - // dc = new DCDate("2010-04"); - // assertThat("testGetHourGMT 3", dc.getHourGMT(), equalTo(0)); - - // One hour out, returns 23 - // dc = new DCDate("2010-04-14"); - // assertThat("testGetHourGMT 4", dc.getHourGMT(), equalTo(0)); - - dc = new DCDate("2010-04-14T01:00:00Z"); - assertThat("testGetHourGMT 5", dc.getHourGMT(), equalTo(1)); - } - - /** - * Test of getMinuteGMT method, of class DCDate. - */ - @Test - public void testGetMinuteGMT() - { - dc = new DCDate((String)null); - assertThat("testGetMinuteGMT 0", dc.getMinuteGMT(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetMinuteGMT 1", dc.getMinuteGMT(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetMinuteGMT 2", dc.getMinuteGMT(), equalTo(0)); - - dc = new DCDate("2010-04"); - assertThat("testGetMinuteGMT 3", dc.getMinuteGMT(), equalTo(0)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetMinuteGMT 4", dc.getMinuteGMT(), equalTo(0)); - - dc = new DCDate("2010-04-14T00:01:00Z"); - assertThat("testGetMinuteGMT 5", dc.getMinuteGMT(), equalTo(1)); - } - - /** - * Test of getSecondGMT method, of class DCDate. - */ - @Test - public void testGetSecondGMT() - { - dc = new DCDate((String)null); - assertThat("testGetSecondGMT 0", dc.getSecondGMT(), equalTo(-1)); - - dc = new DCDate(""); - assertThat("testGetSecondGMT 1", dc.getSecondGMT(), equalTo(-1)); - - dc = new DCDate("2010"); - assertThat("testGetSecondGMT 2", dc.getSecondGMT(), equalTo(0)); - - dc = new DCDate("2010-04"); - assertThat("testGetSecondGMT 3", dc.getSecondGMT(), equalTo(0)); - - dc = new DCDate("2010-04-14"); - assertThat("testGetSecondGMT 4", dc.getSecondGMT(), equalTo(0)); - - dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testGetSecondGMT 5", dc.getSecondGMT(), equalTo(1)); - } /** * Test of getMonthName method, of class DCDate. @@ -654,52 +507,6 @@ public class DCDateTest extends AbstractUnitTest equalTo("diciembre")); } - /** - * Test of displayDate method, of class DCDate. - */ - @Test - public void testDisplayDate() - { - dc = new DCDate(""); - assertThat("testToString 0", dc.toString(), equalTo("null")); - - dc = new DCDate("2010"); - assertThat("testToString 1", dc.displayDate(true, true, - new Locale("en_GB")), - equalTo("2010")); - - dc = new DCDate("2010-04"); - assertThat("testToString 2", dc.displayDate(true, true, - new Locale("en_GB")), - equalTo("Apr-2010")); - - dc = new DCDate("2010-04-14"); - assertThat("testToString 3", dc.displayDate(true, true, - new Locale("en_GB")), - equalTo("14-Apr-2010")); - - dc = new DCDate("2010-04-14T00:00:01Z"); - //hour increses in 1 due to locale - assertThat("testToString 4", dc.displayDate(true, true, - new Locale("en_GB")), - equalTo("14-Apr-2010 01:00:01")); - - dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testToString 5", dc.displayDate(false, true, - new Locale("en_GB")), - equalTo("14-Apr-2010")); - - // Get different values depending on locale - // dc = new DCDate("2010-04-14T00:00:01Z"); - // assertThat("testToString 6", dc.displayDate(true, false, - // new Locale("en_GB")), - // equalTo("14-Apr-2010 01:00:01")); - - dc = new DCDate("2010-04-14T00:00:01Z"); - assertThat("testToString 7", dc.displayDate(false, false, - new Locale("en_GB")), - equalTo("14-Apr-2010")); - } /** * Tests concurrency issues with date