/**
*
* This is used to compare date values of signatures. *
* * @author wprinz */ public class EGIZDate { private static final Logger LOG = Logger.getLogger(EGIZDate.class); /** * The year. */ protected int year; /** * The month. */ protected int month; /** * The day. */ protected int day; /** * The hour. */ protected int hour; /** * The minute. */ protected int minute; /** * The second. */ protected int second; /** * Constructor that fills the date with values. * * @param year * The year. * @param month * The month. * @param day * The day. * @param hour * The hour. * @param minute * The minute. * @param second * The second. */ public EGIZDate(int year, int month, int day, int hour, int minute, int second) { this.year = year; this.month = month; this.day = day; this.hour = hour; this.minute = minute; this.second = second; } /** * Parses the date information from a given date value. * ** Usually the date value is one extracted from the value of the SIG_DATE * field. *
* * @param date_value * The date value String. * @return Returns the parsed EGIZDate. An IllegalArgumentException is * thrown if the date String has an illegal format. */ public static EGIZDate parseFromString(String date_value) { // find the according RFC standard and cite it /* * Pattern date_pattern = * Pattern.compile("^\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d(Z|((\\+|\\-)\\d\\d:\\d\\d))?$"); * Matcher date_matcher = date_pattern.matcher(date_value); if * (!date_matcher.matches()) { throw new IllegalArgumentException("The * date_value (" + date_value + ") has an illegal format."); } // for * some strange reasons capture groups don't work * * int year = Integer.parseInt(date_value.substring(0, 4)); int month = * Integer.parseInt(date_value.substring(5, 7)); int day = * Integer.parseInt(date_value.substring(8, 10)); int hour = * Integer.parseInt(date_value.substring(11, 13)); int minute = * Integer.parseInt(date_value.substring(14, 16)); int second = * Integer.parseInt(date_value.substring(17, 19)); return new * EGIZDate(year, month, day, hour, minute, second); */ String[] parsePatterns = { "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ssZZ" }; if (date_value.length() > 19) { int li = date_value.lastIndexOf(":"); if (li >= 19) { date_value = new StringBuffer(date_value).deleteCharAt(li).toString(); } // FIXME: @iaik: wenn man bei UTC+"Z" die letzten -2 Zeichen durch "UTC" ersetzt verliert die Zeit ihre Sekunden-Einerstelle!!!!!!! // alter code: // if (date_value.endsWith("Z")) { // date_value = date_value.substring(0, date_value.length()-2) + "UTC"; // } // neuer code: if (date_value.endsWith("ZZ")) { date_value = date_value.substring(0, date_value.length()-2) + "UTC"; } if (date_value.endsWith("Z")) { date_value = date_value.substring(0, date_value.length()-1) + "UTC"; } } else { date_value += "UTC"; } Date date; try { date = DateUtils.parseDate(date_value, parsePatterns); } catch (ParseException e) { throw new IllegalArgumentException("The date_value (" + date_value + ") has an illegal format."); } Calendar calendar = new GregorianCalendar(); calendar.setTime(date); LOG.debug("Parsing date string \"" + date_value + "\" returns: " + date); return new EGIZDate(calendar.get(Calendar.YEAR), calendar .get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); } /** * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object obj) { if (!(obj instanceof EGIZDate)) { return false; } return toString().equals(obj.toString()); } /** * @see java.lang.Object#hashCode() */ public int hashCode() { return toString().hashCode(); } /** * @see java.lang.Object#toString() */ public String toString() { return "parsed date = " + year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second; } /** * Converts the date to a long integer. * ** An earlier date is lower than a later date. *
** E.g. a date in 1999 will get a smaller number than a date in 2006. *
* * @return Returns the compareable long. */ protected long toCompareableLong() { return +this.year * 12 * 31 * 24 * 60 * 60 + this.month * 31 * 24 * 60 * 60 + this.day * 24 * 60 * 60 + this.hour * 60 * 60 + this.minute * 60 + this.second; } /** * Compares this EGIZDate to another EXIZDate. * * @param other * The other EGIZDate. * @return Returns negative if this date is earlier (lower) than the other * date. Returns 0 if both dates are equal. Returns positive if this * date is later (higher) than the other date. */ public int compareTo(EGIZDate other) { long diff = toCompareableLong() - other.toCompareableLong(); return (int) diff; } }