Prepare DateUtils for 2.0 release
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137375 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3bfc04d7a3
commit
402c8e0846
|
@ -71,7 +71,7 @@ import java.util.TimeZone;
|
||||||
* @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
|
* @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
* @version $Id: DateUtils.java,v 1.3 2003/06/08 23:14:23 scolebourne Exp $
|
* @version $Id: DateUtils.java,v 1.4 2003/06/23 23:41:10 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class DateUtils {
|
public class DateUtils {
|
||||||
|
|
||||||
|
@ -151,11 +151,35 @@ public class DateUtils {
|
||||||
public final static int RANGE_MONTH_MONDAY = 6;
|
public final static int RANGE_MONTH_MONDAY = 6;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See the other round method. Works with a Date object.
|
* <p><code>DateUtils<code> instances should NOT be constructed in
|
||||||
|
* standard programming. Instead, the class should be used as
|
||||||
|
* <code>DateUtils.parse(str);</code>.</p>
|
||||||
|
*
|
||||||
|
* <p>This constructor is public to permit tools that require a JavaBean
|
||||||
|
* instance to operate.</p>
|
||||||
*/
|
*/
|
||||||
public static Date round(Date val, int field) {
|
public DateUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Round this date, leaving the field specified as the most significant
|
||||||
|
* field. For example, if you had the datetime of 28 Mar 2002
|
||||||
|
* 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
|
||||||
|
* 2002 14:00:00.000. If this was passed with MONTH, it would return
|
||||||
|
* 1 April 2002 0:00:00.000.
|
||||||
|
*
|
||||||
|
* @param date the date to work with
|
||||||
|
* @param field the field from <code>Calendar</code> or SEMI_MONTH
|
||||||
|
* @return the rounded date
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
|
*/
|
||||||
|
public static Date round(Date date, int field) {
|
||||||
|
if (date == null) {
|
||||||
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
|
}
|
||||||
GregorianCalendar gval = new GregorianCalendar();
|
GregorianCalendar gval = new GregorianCalendar();
|
||||||
gval.setTime(val);
|
gval.setTime(date);
|
||||||
modify(gval, field, true);
|
modify(gval, field, true);
|
||||||
return gval.getTime();
|
return gval.getTime();
|
||||||
}
|
}
|
||||||
|
@ -166,33 +190,66 @@ public class DateUtils {
|
||||||
* 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
|
* 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
|
||||||
* 2002 14:00:00.000. If this was passed with MONTH, it would return
|
* 2002 14:00:00.000. If this was passed with MONTH, it would return
|
||||||
* 1 April 2002 0:00:00.000.
|
* 1 April 2002 0:00:00.000.
|
||||||
|
*
|
||||||
|
* @param date the date to work with
|
||||||
|
* @param field the field from <code>Calendar</code> or SEMI_MONTH
|
||||||
|
* @return the rounded date (a different object)
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
*/
|
*/
|
||||||
public static Calendar round(Calendar val, int field) {
|
public static Calendar round(Calendar date, int field) {
|
||||||
Calendar rounded = (Calendar) val.clone();
|
if (date == null) {
|
||||||
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
|
}
|
||||||
|
Calendar rounded = (Calendar) date.clone();
|
||||||
modify(rounded, field, true);
|
modify(rounded, field, true);
|
||||||
return rounded;
|
return rounded;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See the other round method. Works with an Object, trying to
|
* Round this date, leaving the field specified as the most significant
|
||||||
* use it as either a Date or Calendar.
|
* field. For example, if you had the datetime of 28 Mar 2002
|
||||||
|
* 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
|
||||||
|
* 2002 14:00:00.000. If this was passed with MONTH, it would return
|
||||||
|
* 1 April 2002 0:00:00.000.
|
||||||
|
*
|
||||||
|
* @param date the date to work with, either Date or Calendar
|
||||||
|
* @param field the field from <code>Calendar</code> or SEMI_MONTH
|
||||||
|
* @return the rounded date
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
|
* @throws ClassCastException if the object type is not a Date or Calendar
|
||||||
*/
|
*/
|
||||||
public static Date round(Object val, int field) {
|
public static Date round(Object date, int field) {
|
||||||
if (val instanceof Date) {
|
if (date == null) {
|
||||||
return round((Date) val, field);
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
} else if (val instanceof Calendar) {
|
}
|
||||||
return round((Calendar) val, field).getTime();
|
if (date instanceof Date) {
|
||||||
|
return round((Date) date, field);
|
||||||
|
} else if (date instanceof Calendar) {
|
||||||
|
return round((Calendar) date, field).getTime();
|
||||||
} else {
|
} else {
|
||||||
throw new ClassCastException("Could not round " + val);
|
throw new ClassCastException("Could not round " + date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* See the other trunc method. Works with a Date.
|
* Truncate this date, leaving the field specified as the most significant
|
||||||
|
* field. For example, if you had the datetime of 28 Mar 2002
|
||||||
|
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
|
||||||
|
* 2002 13:00:00.000. If this was passed with MONTH, it would return
|
||||||
|
* 1 Mar 2002 0:00:00.000.
|
||||||
|
*
|
||||||
|
* @param date the date to work with
|
||||||
|
* @param field the field from <code>Calendar</code> or SEMI_MONTH
|
||||||
|
* @return the rounded date
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
*/
|
*/
|
||||||
public static Date trunc(Date val, int field) {
|
public static Date truncate(Date date, int field) {
|
||||||
|
if (date == null) {
|
||||||
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
|
}
|
||||||
GregorianCalendar gval = new GregorianCalendar();
|
GregorianCalendar gval = new GregorianCalendar();
|
||||||
gval.setTime(val);
|
gval.setTime(date);
|
||||||
modify(gval, field, false);
|
modify(gval, field, false);
|
||||||
return gval.getTime();
|
return gval.getTime();
|
||||||
}
|
}
|
||||||
|
@ -203,27 +260,55 @@ public class DateUtils {
|
||||||
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
|
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
|
||||||
* 2002 13:00:00.000. If this was passed with MONTH, it would return
|
* 2002 13:00:00.000. If this was passed with MONTH, it would return
|
||||||
* 1 Mar 2002 0:00:00.000.
|
* 1 Mar 2002 0:00:00.000.
|
||||||
|
*
|
||||||
|
* @param date the date to work with
|
||||||
|
* @param field the field from <code>Calendar</code> or SEMI_MONTH
|
||||||
|
* @return the rounded date (a different object)
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
*/
|
*/
|
||||||
public static Calendar trunc(Calendar val, int field) {
|
public static Calendar truncate(Calendar date, int field) {
|
||||||
Calendar truncated = (Calendar) val.clone();
|
if (date == null) {
|
||||||
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
|
}
|
||||||
|
Calendar truncated = (Calendar) date.clone();
|
||||||
modify(truncated, field, false);
|
modify(truncated, field, false);
|
||||||
return truncated;
|
return truncated;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See the other trunc method. Works with an Object, trying to
|
* Truncate this date, leaving the field specified as the most significant
|
||||||
* use it as either a Date or Calendar.
|
* field. For example, if you had the datetime of 28 Mar 2002
|
||||||
|
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
|
||||||
|
* 2002 13:00:00.000. If this was passed with MONTH, it would return
|
||||||
|
* 1 Mar 2002 0:00:00.000.
|
||||||
|
*
|
||||||
|
* @param date the date to work with, either Date or Calendar
|
||||||
|
* @param field the field from <code>Calendar</code> or SEMI_MONTH
|
||||||
|
* @return the rounded date
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
|
* @throws ClassCastException if the object type is not a Date or Calendar
|
||||||
*/
|
*/
|
||||||
public static Date trunc(Object val, int field) {
|
public static Date truncate(Object date, int field) {
|
||||||
if (val instanceof Date) {
|
if (date == null) {
|
||||||
return trunc((Date) val, field);
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
} else if (val instanceof Calendar) {
|
}
|
||||||
return trunc((Calendar) val, field).getTime();
|
if (date instanceof Date) {
|
||||||
|
return truncate((Date) date, field);
|
||||||
|
} else if (date instanceof Calendar) {
|
||||||
|
return truncate((Calendar) date, field).getTime();
|
||||||
} else {
|
} else {
|
||||||
throw new ClassCastException("Could not trunc " + val);
|
throw new ClassCastException("Could not truncate " + date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* Internal calculation method
|
||||||
|
*
|
||||||
|
* @param val the calendar
|
||||||
|
* @param field the field constant
|
||||||
|
* @param round true to round, false to truncate
|
||||||
|
*/
|
||||||
private static void modify(Calendar val, int field, boolean round) {
|
private static void modify(Calendar val, int field, boolean round) {
|
||||||
boolean roundUp = false;
|
boolean roundUp = false;
|
||||||
for (int i = 0; i < fields.length; i++) {
|
for (int i = 0; i < fields.length; i++) {
|
||||||
|
@ -295,26 +380,39 @@ public class DateUtils {
|
||||||
//We need to remove this field
|
//We need to remove this field
|
||||||
val.add(fields[i][0], -offset);
|
val.add(fields[i][0], -offset);
|
||||||
}
|
}
|
||||||
throw new RuntimeException("We do not support that field.");
|
throw new IllegalArgumentException("The field " + field + " is not supported");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Parses strings the way that CVS supports it (very human readable).
|
* Parses a date string formatted in CVS format.
|
||||||
|
*
|
||||||
|
* @param dateStr the date to parse
|
||||||
|
* @return the parsed date
|
||||||
|
* @throws IllegalArgumentException if the date cannot be parsed
|
||||||
*/
|
*/
|
||||||
public static Calendar parse(String original) {
|
public static Calendar parseCVS(String dateStr) {
|
||||||
return parse(original, Locale.getDefault());
|
return parseCVS(dateStr, Locale.getDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses strings the way that CVS supports it (very human readable).
|
* Parses a date string formatted in CVS format.
|
||||||
|
*
|
||||||
|
* @param dateStr the date to parse
|
||||||
|
* @param locale the locale to parse in
|
||||||
|
* @return the parsed date
|
||||||
|
* @throws IllegalArgumentException if the date is null or cannot be parsed
|
||||||
*/
|
*/
|
||||||
public static Calendar parse(String original, Locale locale) {
|
public static Calendar parseCVS(String dateStr, Locale locale) {
|
||||||
|
if (dateStr == null) {
|
||||||
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
|
}
|
||||||
//Get the symbol names
|
//Get the symbol names
|
||||||
DateFormatSymbols symbols = new DateFormatSymbols(locale);
|
DateFormatSymbols symbols = new DateFormatSymbols(locale);
|
||||||
|
|
||||||
//Prep the string to parse
|
//Prep the string to parse
|
||||||
String value = original.toLowerCase().trim();
|
String value = dateStr.toLowerCase().trim();
|
||||||
|
|
||||||
//Get the current date/time
|
//Get the current date/time
|
||||||
Calendar now = Calendar.getInstance();
|
Calendar now = Calendar.getInstance();
|
||||||
|
@ -326,7 +424,7 @@ public class DateUtils {
|
||||||
//Split the value and unit
|
//Split the value and unit
|
||||||
int start = value.indexOf(" ");
|
int start = value.indexOf(" ");
|
||||||
if (start < 0) {
|
if (start < 0) {
|
||||||
throw new RuntimeException("Could not find space in between value and unit");
|
throw new IllegalArgumentException("Could not find space in between value and unit");
|
||||||
}
|
}
|
||||||
String unit = value.substring(start + 1);
|
String unit = value.substring(start + 1);
|
||||||
value = value.substring(0, start);
|
value = value.substring(0, start);
|
||||||
|
@ -358,7 +456,7 @@ public class DateUtils {
|
||||||
} else if (unit.equals("years") || unit.equals("year")) {
|
} else if (unit.equals("years") || unit.equals("year")) {
|
||||||
now.add(Calendar.YEAR, -val);
|
now.add(Calendar.YEAR, -val);
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("We do not understand that many units ago");
|
throw new IllegalArgumentException("We do not understand that many units ago");
|
||||||
}
|
}
|
||||||
return now;
|
return now;
|
||||||
} else if (value.startsWith("last ")) {
|
} else if (value.startsWith("last ")) {
|
||||||
|
@ -404,7 +502,7 @@ public class DateUtils {
|
||||||
//Try to parse the date a number of different ways
|
//Try to parse the date a number of different ways
|
||||||
for (int i = 0; i < dateFormats.length; i++) {
|
for (int i = 0; i < dateFormats.length; i++) {
|
||||||
try {
|
try {
|
||||||
Date datetime = dateFormats[i].parse(original);
|
Date datetime = dateFormats[i].parse(dateStr);
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(datetime);
|
cal.setTime(datetime);
|
||||||
return cal;
|
return cal;
|
||||||
|
@ -413,7 +511,29 @@ public class DateUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException("Unable to parse '" + original + "'.");
|
throw new IllegalArgumentException("Unable to parse '" + dateStr + "'.");
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* This constructs an Iterator that will start and stop over a date
|
||||||
|
* range based on the focused date and the range style. For instance,
|
||||||
|
* passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return
|
||||||
|
* an Iterator that starts with Sunday, June 30, 2002 and ends with
|
||||||
|
* Saturday, August 3, 2002.
|
||||||
|
*
|
||||||
|
* @param focus the date to work with
|
||||||
|
* @param rangeStyle the style constant to use
|
||||||
|
* @return the date iterator
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
|
*/
|
||||||
|
public static Iterator iterator(Date focus, int rangeStyle) {
|
||||||
|
if (focus == null) {
|
||||||
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
|
}
|
||||||
|
GregorianCalendar gval = new GregorianCalendar();
|
||||||
|
gval.setTime(focus);
|
||||||
|
return iterator(gval, rangeStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -422,8 +542,16 @@ public class DateUtils {
|
||||||
* passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return
|
* passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return
|
||||||
* an Iterator that starts with Sunday, June 30, 2002 and ends with
|
* an Iterator that starts with Sunday, June 30, 2002 and ends with
|
||||||
* Saturday, August 3, 2002.
|
* Saturday, August 3, 2002.
|
||||||
|
*
|
||||||
|
* @param focus the date to work with
|
||||||
|
* @param rangeStyle the style constant to use
|
||||||
|
* @return the date iterator
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
*/
|
*/
|
||||||
public static Iterator getCalendarIterator(Calendar focus, int rangeStyle) {
|
public static Iterator iterator(Calendar focus, int rangeStyle) {
|
||||||
|
if (focus == null) {
|
||||||
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
|
}
|
||||||
Calendar start = null;
|
Calendar start = null;
|
||||||
Calendar end = null;
|
Calendar end = null;
|
||||||
int startCutoff = Calendar.SUNDAY;
|
int startCutoff = Calendar.SUNDAY;
|
||||||
|
@ -432,7 +560,7 @@ public class DateUtils {
|
||||||
case RANGE_MONTH_SUNDAY:
|
case RANGE_MONTH_SUNDAY:
|
||||||
case RANGE_MONTH_MONDAY:
|
case RANGE_MONTH_MONDAY:
|
||||||
//Set start to the first of the month
|
//Set start to the first of the month
|
||||||
start = trunc(focus, Calendar.MONTH);
|
start = truncate(focus, Calendar.MONTH);
|
||||||
//Set end to the last of the month
|
//Set end to the last of the month
|
||||||
end = (Calendar) start.clone();
|
end = (Calendar) start.clone();
|
||||||
end.add(Calendar.MONTH, 1);
|
end.add(Calendar.MONTH, 1);
|
||||||
|
@ -448,8 +576,8 @@ public class DateUtils {
|
||||||
case RANGE_WEEK_RELATIVE:
|
case RANGE_WEEK_RELATIVE:
|
||||||
case RANGE_WEEK_CENTER:
|
case RANGE_WEEK_CENTER:
|
||||||
//Set start and end to the current date
|
//Set start and end to the current date
|
||||||
start = trunc(focus, Calendar.DATE);
|
start = truncate(focus, Calendar.DATE);
|
||||||
end = trunc(focus, Calendar.DATE);
|
end = truncate(focus, Calendar.DATE);
|
||||||
switch (rangeStyle) {
|
switch (rangeStyle) {
|
||||||
case RANGE_WEEK_SUNDAY:
|
case RANGE_WEEK_SUNDAY:
|
||||||
//already set by default
|
//already set by default
|
||||||
|
@ -469,7 +597,7 @@ public class DateUtils {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("The range style " + rangeStyle + " is not valid.");
|
throw new IllegalArgumentException("The range style " + rangeStyle + " is not valid.");
|
||||||
}
|
}
|
||||||
if (startCutoff < Calendar.SUNDAY) {
|
if (startCutoff < Calendar.SUNDAY) {
|
||||||
startCutoff += 7;
|
startCutoff += 7;
|
||||||
|
@ -489,55 +617,66 @@ public class DateUtils {
|
||||||
while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) {
|
while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) {
|
||||||
end.add(Calendar.DATE, 1);
|
end.add(Calendar.DATE, 1);
|
||||||
}
|
}
|
||||||
final Calendar startFinal = start;
|
return new DateIterator(start, end);
|
||||||
final Calendar endFinal = end;
|
|
||||||
Iterator it = new Iterator() {
|
|
||||||
Calendar spot = null;
|
|
||||||
{
|
|
||||||
spot = startFinal;
|
|
||||||
spot.add(Calendar.DATE, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasNext() {
|
|
||||||
return spot.before(endFinal);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object next() {
|
|
||||||
if (spot.equals(endFinal)) {
|
|
||||||
throw new NoSuchElementException();
|
|
||||||
}
|
|
||||||
spot.add(Calendar.DATE, 1);
|
|
||||||
return spot.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return it;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See the other getCalendarIterator. Works with a Date.
|
* This constructs an Iterator that will start and stop over a date
|
||||||
|
* range based on the focused date and the range style. For instance,
|
||||||
|
* passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return
|
||||||
|
* an Iterator that starts with Sunday, June 30, 2002 and ends with
|
||||||
|
* Saturday, August 3, 2002.
|
||||||
|
*
|
||||||
|
* @param focus the date to work with, either Date or Calendar
|
||||||
|
* @param rangeStyle the style constant to use
|
||||||
|
* @return the date iterator
|
||||||
|
* @throws IllegalArgumentException if the date is null
|
||||||
|
* @throws ClassCastException if the object type is not a Date or Calendar
|
||||||
*/
|
*/
|
||||||
public static Iterator getCalendarIterator(Date focus, int rangeStyle) {
|
public static Iterator iterator(Object focus, int rangeStyle) {
|
||||||
GregorianCalendar gval = new GregorianCalendar();
|
if (focus == null) {
|
||||||
gval.setTime(focus);
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
return getCalendarIterator(gval, rangeStyle);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* See the other getCalendarIterator. Works with an Object, trying
|
|
||||||
* to use it as a Date or Calendar.
|
|
||||||
*/
|
|
||||||
public static Iterator getCalendarIterator(Object focus, int rangeStyle) {
|
|
||||||
if (focus instanceof Date) {
|
if (focus instanceof Date) {
|
||||||
return getCalendarIterator((Date) focus, rangeStyle);
|
return iterator((Date) focus, rangeStyle);
|
||||||
} else if (focus instanceof Calendar) {
|
} else if (focus instanceof Calendar) {
|
||||||
return getCalendarIterator((Calendar) focus, rangeStyle);
|
return iterator((Calendar) focus, rangeStyle);
|
||||||
} else {
|
} else {
|
||||||
throw new ClassCastException("Could not iterate based on " + focus);
|
throw new ClassCastException("Could not iterate based on " + focus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date iterator.
|
||||||
|
*/
|
||||||
|
static class DateIterator implements Iterator {
|
||||||
|
private final Calendar startFinal;
|
||||||
|
private final Calendar endFinal;
|
||||||
|
private Calendar spot = null;
|
||||||
|
|
||||||
|
DateIterator(Calendar startFinal, Calendar endFinal) {
|
||||||
|
super();
|
||||||
|
this.startFinal = startFinal;
|
||||||
|
this.endFinal = endFinal;
|
||||||
|
spot = startFinal;
|
||||||
|
spot.add(Calendar.DATE, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasNext() {
|
||||||
|
return spot.before(endFinal);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object next() {
|
||||||
|
if (spot.equals(endFinal)) {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
spot.add(Calendar.DATE, 1);
|
||||||
|
return spot.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,62 +153,104 @@ public class DateUtilsTest extends TestCase {
|
||||||
assertEquals("round second-2 failed",
|
assertEquals("round second-2 failed",
|
||||||
dateTimeParser.parse("November 18, 2001 1:23:11.000"),
|
dateTimeParser.parse("November 18, 2001 1:23:11.000"),
|
||||||
DateUtils.round(date2, Calendar.SECOND));
|
DateUtils.round(date2, Calendar.SECOND));
|
||||||
|
|
||||||
|
try {
|
||||||
|
DateUtils.round((Date) null, Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.round((Calendar) null, Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.round((Object) null, Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.round("", Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (ClassCastException ex) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests various values with the trunc method
|
* Tests various values with the trunc method
|
||||||
*/
|
*/
|
||||||
public void testTrunc() throws Exception {
|
public void testTruncate() throws Exception {
|
||||||
assertEquals("trunc year-1 failed",
|
assertEquals("truncate year-1 failed",
|
||||||
dateParser.parse("January 1, 2002"),
|
dateParser.parse("January 1, 2002"),
|
||||||
DateUtils.trunc(date1, Calendar.YEAR));
|
DateUtils.truncate(date1, Calendar.YEAR));
|
||||||
assertEquals("trunc year-2 failed",
|
assertEquals("truncate year-2 failed",
|
||||||
dateParser.parse("January 1, 2001"),
|
dateParser.parse("January 1, 2001"),
|
||||||
DateUtils.trunc(date2, Calendar.YEAR));
|
DateUtils.truncate(date2, Calendar.YEAR));
|
||||||
assertEquals("trunc month-1 failed",
|
assertEquals("truncate month-1 failed",
|
||||||
dateParser.parse("February 1, 2002"),
|
dateParser.parse("February 1, 2002"),
|
||||||
DateUtils.trunc(date1, Calendar.MONTH));
|
DateUtils.truncate(date1, Calendar.MONTH));
|
||||||
assertEquals("trunc month-2 failed",
|
assertEquals("truncate month-2 failed",
|
||||||
dateParser.parse("November 1, 2001"),
|
dateParser.parse("November 1, 2001"),
|
||||||
DateUtils.trunc(date2, Calendar.MONTH));
|
DateUtils.truncate(date2, Calendar.MONTH));
|
||||||
assertEquals("trunc semimonth-1 failed",
|
assertEquals("truncate semimonth-1 failed",
|
||||||
dateParser.parse("February 1, 2002"),
|
dateParser.parse("February 1, 2002"),
|
||||||
DateUtils.trunc(date1, DateUtils.SEMI_MONTH));
|
DateUtils.truncate(date1, DateUtils.SEMI_MONTH));
|
||||||
assertEquals("trunc semimonth-2 failed",
|
assertEquals("truncate semimonth-2 failed",
|
||||||
dateParser.parse("November 16, 2001"),
|
dateParser.parse("November 16, 2001"),
|
||||||
DateUtils.trunc(date2, DateUtils.SEMI_MONTH));
|
DateUtils.truncate(date2, DateUtils.SEMI_MONTH));
|
||||||
assertEquals("trunc date-1 failed",
|
assertEquals("truncate date-1 failed",
|
||||||
dateParser.parse("February 12, 2002"),
|
dateParser.parse("February 12, 2002"),
|
||||||
DateUtils.trunc(date1, Calendar.DATE));
|
DateUtils.truncate(date1, Calendar.DATE));
|
||||||
assertEquals("trunc date-2 failed",
|
assertEquals("truncate date-2 failed",
|
||||||
dateParser.parse("November 18, 2001"),
|
dateParser.parse("November 18, 2001"),
|
||||||
DateUtils.trunc(date2, Calendar.DATE));
|
DateUtils.truncate(date2, Calendar.DATE));
|
||||||
assertEquals("trunc hour-1 failed",
|
assertEquals("truncate hour-1 failed",
|
||||||
dateTimeParser.parse("February 12, 2002 12:00:00.000"),
|
dateTimeParser.parse("February 12, 2002 12:00:00.000"),
|
||||||
DateUtils.trunc(date1, Calendar.HOUR));
|
DateUtils.truncate(date1, Calendar.HOUR));
|
||||||
assertEquals("trunc hour-2 failed",
|
assertEquals("truncate hour-2 failed",
|
||||||
dateTimeParser.parse("November 18, 2001 1:00:00.000"),
|
dateTimeParser.parse("November 18, 2001 1:00:00.000"),
|
||||||
DateUtils.trunc(date2, Calendar.HOUR));
|
DateUtils.truncate(date2, Calendar.HOUR));
|
||||||
assertEquals("trunc minute-1 failed",
|
assertEquals("truncate minute-1 failed",
|
||||||
dateTimeParser.parse("February 12, 2002 12:34:00.000"),
|
dateTimeParser.parse("February 12, 2002 12:34:00.000"),
|
||||||
DateUtils.trunc(date1, Calendar.MINUTE));
|
DateUtils.truncate(date1, Calendar.MINUTE));
|
||||||
assertEquals("trunc minute-2 failed",
|
assertEquals("truncate minute-2 failed",
|
||||||
dateTimeParser.parse("November 18, 2001 1:23:00.000"),
|
dateTimeParser.parse("November 18, 2001 1:23:00.000"),
|
||||||
DateUtils.trunc(date2, Calendar.MINUTE));
|
DateUtils.truncate(date2, Calendar.MINUTE));
|
||||||
assertEquals("trunc second-1 failed",
|
assertEquals("truncate second-1 failed",
|
||||||
dateTimeParser.parse("February 12, 2002 12:34:56.000"),
|
dateTimeParser.parse("February 12, 2002 12:34:56.000"),
|
||||||
DateUtils.trunc(date1, Calendar.SECOND));
|
DateUtils.truncate(date1, Calendar.SECOND));
|
||||||
assertEquals("trunc second-2 failed",
|
assertEquals("truncate second-2 failed",
|
||||||
dateTimeParser.parse("November 18, 2001 1:23:11.000"),
|
dateTimeParser.parse("November 18, 2001 1:23:11.000"),
|
||||||
DateUtils.trunc(date2, Calendar.SECOND));
|
DateUtils.truncate(date2, Calendar.SECOND));
|
||||||
|
|
||||||
|
try {
|
||||||
|
DateUtils.truncate((Date) null, Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.truncate((Calendar) null, Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.truncate((Object) null, Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.truncate("", Calendar.SECOND);
|
||||||
|
fail();
|
||||||
|
} catch (ClassCastException ex) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the parse method, which is supposed to handle various strings
|
* Tests the parse method, which is supposed to handle various strings
|
||||||
* as flexibly as CVS supports.
|
* as flexibly as CVS supports.
|
||||||
*/
|
*/
|
||||||
public void testParse() throws Exception {
|
public void testParseCVS() throws Exception {
|
||||||
|
try {
|
||||||
|
DateUtils.parseCVS(null);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.parseCVS("gobbledegook");
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
|
||||||
//This is difficult to test since the "now" used in the
|
//This is difficult to test since the "now" used in the
|
||||||
// parse function cannot be controlled. We could possibly control
|
// parse function cannot be controlled. We could possibly control
|
||||||
// it by trying before and after and making sure the value we expect
|
// it by trying before and after and making sure the value we expect
|
||||||
|
@ -219,22 +261,22 @@ public class DateUtilsTest extends TestCase {
|
||||||
|
|
||||||
now = Calendar.getInstance();
|
now = Calendar.getInstance();
|
||||||
now.add(Calendar.MINUTE, -1);
|
now.add(Calendar.MINUTE, -1);
|
||||||
assertEquals("parse 1 minute ago",
|
assertEquals("parseCVS 1 minute ago",
|
||||||
now, DateUtils.parse("1 minute ago"), 50);
|
now, DateUtils.parseCVS("1 minute ago"), 50);
|
||||||
now = Calendar.getInstance();
|
now = Calendar.getInstance();
|
||||||
now.add(Calendar.MINUTE, -8);
|
now.add(Calendar.MINUTE, -8);
|
||||||
assertEquals("parse 8 minutes ago",
|
assertEquals("parseCVS 8 minutes ago",
|
||||||
now, DateUtils.parse("8 minutes ago"), 50);
|
now, DateUtils.parseCVS("8 minutes ago"), 50);
|
||||||
|
|
||||||
now = Calendar.getInstance();
|
now = Calendar.getInstance();
|
||||||
now.add(Calendar.DATE, -1);
|
now.add(Calendar.DATE, -1);
|
||||||
assertEquals("parse yesterday",
|
assertEquals("parseCVS yesterday",
|
||||||
now, DateUtils.parse("yesterday"), 50);
|
now, DateUtils.parseCVS("yesterday"), 50);
|
||||||
|
|
||||||
now = Calendar.getInstance();
|
now = Calendar.getInstance();
|
||||||
now.add(Calendar.DATE, 1);
|
now.add(Calendar.DATE, 1);
|
||||||
assertEquals("parse tomorrow",
|
assertEquals("parseCVS tomorrow",
|
||||||
now, DateUtils.parse("tomorrow"), 50);
|
now, DateUtils.parseCVS("tomorrow"), 50);
|
||||||
|
|
||||||
now = Calendar.getInstance();
|
now = Calendar.getInstance();
|
||||||
//Sunday would be 1, Saturday would be 7, so we walk back up to 6 days.
|
//Sunday would be 1, Saturday would be 7, so we walk back up to 6 days.
|
||||||
|
@ -244,13 +286,13 @@ public class DateUtilsTest extends TestCase {
|
||||||
} else {
|
} else {
|
||||||
now.add(Calendar.DATE, 1 - now.get(Calendar.DAY_OF_WEEK));
|
now.add(Calendar.DATE, 1 - now.get(Calendar.DAY_OF_WEEK));
|
||||||
}
|
}
|
||||||
assertEquals("parse last Sunday",
|
assertEquals("parseCVS last Sunday",
|
||||||
now, DateUtils.parse("last Sunday"), 50);
|
now, DateUtils.parseCVS("last Sunday"), 50);
|
||||||
|
|
||||||
now = Calendar.getInstance();
|
now = Calendar.getInstance();
|
||||||
now.add(Calendar.DATE, -7);
|
now.add(Calendar.DATE, -7);
|
||||||
assertEquals("parse last week",
|
assertEquals("parseCVS last week",
|
||||||
now, DateUtils.parse("last week"), 50);
|
now, DateUtils.parseCVS("last week"), 50);
|
||||||
|
|
||||||
now = Calendar.getInstance();
|
now = Calendar.getInstance();
|
||||||
//January would be 0, December would be 11, so we walk back up to 11 months
|
//January would be 0, December would be 11, so we walk back up to 11 months
|
||||||
|
@ -260,8 +302,30 @@ public class DateUtilsTest extends TestCase {
|
||||||
} else {
|
} else {
|
||||||
now.add(Calendar.MONTH, 0 - now.get(Calendar.MONTH));
|
now.add(Calendar.MONTH, 0 - now.get(Calendar.MONTH));
|
||||||
}
|
}
|
||||||
assertEquals("parse last January",
|
assertEquals("parseCVS last January",
|
||||||
now, DateUtils.parse("last January"), 50);
|
now, DateUtils.parseCVS("last January"), 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the iterator exceptions
|
||||||
|
*/
|
||||||
|
public void testIteratorEx() throws Exception {
|
||||||
|
try {
|
||||||
|
DateUtils.iterator((Date) null, DateUtils.RANGE_WEEK_CENTER);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.iterator((Calendar) null, DateUtils.RANGE_WEEK_CENTER);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.iterator((Object) null, DateUtils.RANGE_WEEK_CENTER);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
DateUtils.iterator("", DateUtils.RANGE_WEEK_CENTER);
|
||||||
|
fail();
|
||||||
|
} catch (ClassCastException ex) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -270,26 +334,26 @@ public class DateUtilsTest extends TestCase {
|
||||||
public void testWeekIterator() throws Exception {
|
public void testWeekIterator() throws Exception {
|
||||||
Calendar now = Calendar.getInstance();
|
Calendar now = Calendar.getInstance();
|
||||||
for (int i = 0; i< 7; i++) {
|
for (int i = 0; i< 7; i++) {
|
||||||
Calendar today = DateUtils.trunc(now, Calendar.DATE);
|
Calendar today = DateUtils.truncate(now, Calendar.DATE);
|
||||||
Calendar sunday = DateUtils.trunc(now, Calendar.DATE);
|
Calendar sunday = DateUtils.truncate(now, Calendar.DATE);
|
||||||
sunday.add(Calendar.DATE, 1 - sunday.get(Calendar.DAY_OF_WEEK));
|
sunday.add(Calendar.DATE, 1 - sunday.get(Calendar.DAY_OF_WEEK));
|
||||||
Calendar monday = DateUtils.trunc(now, Calendar.DATE);
|
Calendar monday = DateUtils.truncate(now, Calendar.DATE);
|
||||||
if (monday.get(Calendar.DAY_OF_WEEK) == 1) {
|
if (monday.get(Calendar.DAY_OF_WEEK) == 1) {
|
||||||
//This is sunday... roll back 6 days
|
//This is sunday... roll back 6 days
|
||||||
monday.add(Calendar.DATE, -6);
|
monday.add(Calendar.DATE, -6);
|
||||||
} else {
|
} else {
|
||||||
monday.add(Calendar.DATE, 2 - monday.get(Calendar.DAY_OF_WEEK));
|
monday.add(Calendar.DATE, 2 - monday.get(Calendar.DAY_OF_WEEK));
|
||||||
}
|
}
|
||||||
Calendar centered = DateUtils.trunc(now, Calendar.DATE);
|
Calendar centered = DateUtils.truncate(now, Calendar.DATE);
|
||||||
centered.add(Calendar.DATE, -3);
|
centered.add(Calendar.DATE, -3);
|
||||||
|
|
||||||
Iterator it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_SUNDAY);
|
Iterator it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_SUNDAY);
|
||||||
assertWeekIterator(it, sunday);
|
assertWeekIterator(it, sunday);
|
||||||
it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_MONDAY);
|
it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_MONDAY);
|
||||||
assertWeekIterator(it, monday);
|
assertWeekIterator(it, monday);
|
||||||
it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_RELATIVE);
|
it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_RELATIVE);
|
||||||
assertWeekIterator(it, today);
|
assertWeekIterator(it, today);
|
||||||
it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_CENTER);
|
it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER);
|
||||||
assertWeekIterator(it, centered);
|
assertWeekIterator(it, centered);
|
||||||
now.add(Calendar.DATE,1);
|
now.add(Calendar.DATE,1);
|
||||||
}
|
}
|
||||||
|
@ -299,22 +363,22 @@ public class DateUtilsTest extends TestCase {
|
||||||
* Tests the calendar iterator for month-based ranges
|
* Tests the calendar iterator for month-based ranges
|
||||||
*/
|
*/
|
||||||
public void testMonthIterator() throws Exception {
|
public void testMonthIterator() throws Exception {
|
||||||
Iterator it = DateUtils.getCalendarIterator(date1, DateUtils.RANGE_MONTH_SUNDAY);
|
Iterator it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_SUNDAY);
|
||||||
assertWeekIterator(it,
|
assertWeekIterator(it,
|
||||||
dateParser.parse("January 27, 2002"),
|
dateParser.parse("January 27, 2002"),
|
||||||
dateParser.parse("March 2, 2002"));
|
dateParser.parse("March 2, 2002"));
|
||||||
|
|
||||||
it = DateUtils.getCalendarIterator(date1, DateUtils.RANGE_MONTH_MONDAY);
|
it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_MONDAY);
|
||||||
assertWeekIterator(it,
|
assertWeekIterator(it,
|
||||||
dateParser.parse("January 28, 2002"),
|
dateParser.parse("January 28, 2002"),
|
||||||
dateParser.parse("March 3, 2002"));
|
dateParser.parse("March 3, 2002"));
|
||||||
|
|
||||||
it = DateUtils.getCalendarIterator(date2, DateUtils.RANGE_MONTH_SUNDAY);
|
it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_SUNDAY);
|
||||||
assertWeekIterator(it,
|
assertWeekIterator(it,
|
||||||
dateParser.parse("October 28, 2001"),
|
dateParser.parse("October 28, 2001"),
|
||||||
dateParser.parse("December 1, 2001"));
|
dateParser.parse("December 1, 2001"));
|
||||||
|
|
||||||
it = DateUtils.getCalendarIterator(date2, DateUtils.RANGE_MONTH_MONDAY);
|
it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_MONDAY);
|
||||||
assertWeekIterator(it,
|
assertWeekIterator(it,
|
||||||
dateParser.parse("October 29, 2001"),
|
dateParser.parse("October 29, 2001"),
|
||||||
dateParser.parse("December 2, 2001"));
|
dateParser.parse("December 2, 2001"));
|
||||||
|
@ -355,7 +419,7 @@ public class DateUtilsTest extends TestCase {
|
||||||
int count = 1;
|
int count = 1;
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
//Check this is just a date (no time component)
|
//Check this is just a date (no time component)
|
||||||
assertEquals("", cal, DateUtils.trunc(cal, Calendar.DATE), 0);
|
assertEquals("", cal, DateUtils.truncate(cal, Calendar.DATE), 0);
|
||||||
|
|
||||||
last = cal;
|
last = cal;
|
||||||
cal = (Calendar) it.next();
|
cal = (Calendar) it.next();
|
||||||
|
|
Loading…
Reference in New Issue