| 
      
      
      From: <pat...@us...> - 2009-10-20 17:37:11
      
     | 
| Revision: 972
          http://cishell.svn.sourceforge.net/cishell/?rev=972&view=rev
Author:   pataphil
Date:     2009-10-20 17:37:04 +0000 (Tue, 20 Oct 2009)
Log Message:
-----------
DateUtilities.parseDate now has flavors where the year can be "fixed".  (Java's built-in Date class subtracts 1900 from the year by default; parseDate optionally "fixes" the year by adding 1900 back to it.)
Modified Paths:
--------------
    trunk/core/org.cishell.utilities/src/org/cishell/utilities/DateUtilities.java
Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/DateUtilities.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/DateUtilities.java	2009-10-19 18:50:27 UTC (rev 971)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/DateUtilities.java	2009-10-20 17:37:04 UTC (rev 972)
@@ -223,33 +223,50 @@
 	};
 	
 	public static Date parseDate(String dateString) throws ParseException {
+		return parseDate(dateString, true);
+	}
+	
+	public static Date parseDate(String dateString, boolean fixYear)
+			throws ParseException {
 		return (parseDate(dateString, MONTH_DAY_YEAR_DATE_FORMATS));
 	}
 	
 	public static Date parseDate(String dateString, String suggestedDateFormat)
 			throws ParseException {
+		return parseDate(dateString, suggestedDateFormat, true);
+	}
+	
+	public static Date parseDate(
+			String dateString, String suggestedDateFormat, boolean fixYear)
+			throws ParseException {
 		if (MONTH_DAY_YEAR_DATE_FORMAT.equals(suggestedDateFormat)) {
-			return parseDate(dateString, MONTH_DAY_YEAR_DATE_FORMATS);
+			return parseDate(dateString, MONTH_DAY_YEAR_DATE_FORMATS, fixYear);
 		} else if (DAY_MONTH_YEAR_DATE_FORMAT.equals(suggestedDateFormat)) {
-			return parseDate(dateString, DAY_MONTH_YEAR_DATE_FORMATS);
+			return parseDate(dateString, DAY_MONTH_YEAR_DATE_FORMATS, fixYear);
 		} else {
 			DateFormat[] dateFormats = new DateFormat[] {
 				new SimpleDateFormat(suggestedDateFormat)
 			};
 			
-			return parseDate(dateString, dateFormats);
+			return parseDate(dateString, dateFormats, fixYear);
 		}
 	}
 	
 	public static Date parseDate(String dateString, DateFormat[] dateFormats)
 			throws ParseException {
+		return parseDate(dateString, dateFormats, true);
+	}
+	
+	public static Date parseDate(
+			String dateString, DateFormat[] dateFormats, boolean fixYear)
+			throws ParseException {
 		for (int ii = 0; ii < dateFormats.length; ii++) {
 			try {
 				DateFormat format = dateFormats[ii];
 				format.setLenient(false);
 				Date date = format.parse(dateString);
 				
-				if (date.getYear() < 1900) {
+				if (fixYear && (date.getYear() < 1900)) {
 					date.setYear(date.getYear() + 1900);
 				}
 				
@@ -274,6 +291,12 @@
 	
 	public static Date interpretObjectAsDate(Object object, String dateFormat)
 			throws ParseException {
+		return interpretObjectAsDate(object, dateFormat, true);
+	}
+	
+	public static Date interpretObjectAsDate(
+			Object object, String dateFormat, boolean fixYear)
+			throws ParseException {
 		final String EMPTY_DATE_MESSAGE = "An empty date was found.";
 		
 		String objectAsString = object.toString();
@@ -355,37 +378,9 @@
 			}
 		}
 		
-		return parseDate(objectAsString, dateFormat);
+		return parseDate(objectAsString, dateFormat, fixYear);
 	}
 	
-//	private java.util.Date parseDate(String dateString) 
-//		throws AlgorithmExecutionException {
-//		for (DateFormat format : MONTH_DAY_YEAR_DATE_FORMATS) {
-//			try {
-//				format.setLenient(false);
-//				java.util.Date date = format.parse(dateString);
-//				//WE PARSED THE DATE SUCCESSFULLY (if we get to this point)!
-//				//Finish up our processing and return the date.
-//				
-//				//TODO: Methinks this is a hack we should eliminate
-//				if (date.getYear() < 1900)
-//					date.setYear(date.getYear() + 1900);
-//				java.sql.Date dateForSQL = new java.sql.Date(date.getTime());
-//				return dateForSQL;
-//			} catch (ParseException e) {
-//				continue;
-//			}
-//		}
-//		
-//		//we could not parse the date with any of the accepted formats.
-//		
-//		String exceptionMessage = 
-//			"Could not parse the field " + 
-//			"'" + dateString + "'" +
-//			" as a date. Aborting the algorithm.";
-//		throw new AlgorithmExecutionException(exceptionMessage);
-//	}
-	
 	private static Date fixDateYear(Date date) {
 		if (date.getYear() < 1900) {
 			Date fixedDate = (Date)date.clone();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |