/**
* Regular expression representing all characters to be treated as
* separators in <code>COMPARE_DATETIME</code> mode.<br>
* Current setting: all non-numeric characters</br>
*/
private static final String REGEX_DATETIME_SEPARATOR = "[^0-9]*[^0-9]";
private static final String REGEX_DATETIME_DD = "[^0-9]*[0-9][0-9][^0-9]*";
private static final SimpleDateFormat FORMAT_DATETIME_DD = new SimpleDateFormat("dd");
private static final String REGEX_DATETIME_DDMM = REGEX_DATETIME_DD
+ "[^0-9][0-9][0-9][^0-9]*";
private static final SimpleDateFormat FORMAT_DATETIME_DDMM = new SimpleDateFormat("dd MM");
private static final String REGEX_DATETIME_DDMMYYYY = REGEX_DATETIME_DDMM
+ "[^0-9][0-9][0-9][0-9][0-9][^0-9]*";
private static final SimpleDateFormat FORMAT_DATETIME_DDMMYYYY = new SimpleDateFormat("dd MM yyyy");
private static final String REGEX_DATETIME_DDMM_HHMM = REGEX_DATETIME_DDMM
+ "[^0-9][0-9][0-9][^0-9]*" + "[^0-9][0-9][0-9][^0-9]*";
private static final SimpleDateFormat FORMAT_DATETIME_DDMM_HHMM = new SimpleDateFormat("dd MM HH mm");
private static final String REGEX_DATETIME_DDMMYYYY_HHMM = REGEX_DATETIME_DDMMYYYY
+ "[^0-9][0-9][0-9][^0-9]*" + "[^0-9][0-9][0-9][^0-9]*";
private static final SimpleDateFormat FORMAT_DATETIME_DDMMYYYY_HHMM = new SimpleDateFormat("dd MM yyyy HH mm");
/**
* Compares the values of the given column in the two specified entries as
* date strings in one of the following forms: <code>DD.</code>,
* <code>DD.MM.</code>, <code>DD.MM.YYYY</code>,
* <code>DD.MM.YYYY_hh:mm</code>, <code>DD.MM_hh:mm</code><br>
* Every character that is no number is considered to be a separator.<br>
* The single values are interpreted as <code>Integer</code>s.
*
* @param targetColumn
* selected column
* @param itemOne
* first entry to compare
* @param itemTwo
* second entry to compare
* @return first entry is smaller, bigger or equals second entry
*/
int compareDateTime(TableColumn targetColumn, TableItem itemOne,
TableItem itemTwo) {
int result;
// get current position of the target column
final int columnIndex = getColumnIndex(targetColumn);
try {
// read contained dates and compare them
result = parseDate(itemOne.getText(columnIndex)).compareTo(
parseDate(itemTwo.getText(columnIndex)));
if (SorTable.this.lastDescendingColumn == targetColumn) {
/*
* column was already sorted in descending order: invert sort
* direction
*/
result *= -1;
}
} catch (final ParseException ex) {
/*
* at least one of the contained strings do not fit on any of the
* given date formats
*/
result = compareText(targetColumn, itemOne, itemTwo);
}
return result;
}
/**
* Reads the given <code>String</code> expecting one of the given date
* formats:<br>
* <code>DD.</code>, <code>DD.MM.</code>, <code>DD.MM.YYYY</code>,
* <code>DD.MM.YYYY_hh:mm</code> or <code>DD.MM._hh:mm</code></br> All
* non-numeric characters are treated to be separators. Empty segments are
* ignored.
*
* @param dateString
* date <code>String</code> to parse
* @return contained date value
* @throws ParseException
* failed to parse given format or <code>String</code> did not
* match any allowed pattern
*/
private Date parseDate(final String dateString) throws ParseException {
// replace separators with whitespaces to use trim()
final String cleanValue = dateString.replaceAll(
REGEX_DATETIME_SEPARATOR, " ").trim();
// get the Formatter for the matching pattern
SimpleDateFormat formatter;
if (Pattern.matches(REGEX_DATETIME_DD, cleanValue)) {
formatter = FORMAT_DATETIME_DD;
} else if (Pattern.matches(REGEX_DATETIME_DDMM, cleanValue)) {
formatter = FORMAT_DATETIME_DDMM;
} else if (Pattern.matches(REGEX_DATETIME_DDMMYYYY, cleanValue)) {
formatter = FORMAT_DATETIME_DDMMYYYY;
} else if (Pattern.matches(REGEX_DATETIME_DDMM_HHMM, cleanValue)) {
formatter = FORMAT_DATETIME_DDMM_HHMM;
} else if (Pattern.matches(REGEX_DATETIME_DDMMYYYY_HHMM, cleanValue)) {
formatter = FORMAT_DATETIME_DDMMYYYY_HHMM;
} else {
// does not fit on any given pattern
throw new ParseException(dateString, 0);
}
// parse the contained date
return formatter.parse(cleanValue);
}