/** * Copyright (c) 2006 by Know-Center, Graz, Austria * * This software is the confidential and proprietary information of Know-Center, * Graz, Austria. You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with Know-Center. * * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR * NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. * * $Id: EGIZDate.java,v 1.1 2006/10/31 08:08:33 wprinz Exp $ */ package at.knowcenter.wag.egov.egiz.pdf; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.time.DateUtils; /** * Represents a signature date and the signing time as can be found in the * SIG_DATE field. * *

* This is used to compare date values of signatures. *

* * @author wprinz */ public class EGIZDate { /** * 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(); } if (date_value.endsWith("Z")) { date_value = date_value.substring(0, date_value.length()-2) + "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); 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; } }