diff --git a/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
index c5cf5619bb16cc8ec33c1a298c607d5072e7a438..949b919541e39fb90ce46b8b37e5e008c7021327 100644
--- a/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
+++ b/src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
@@ -514,7 +514,9 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 		StringBuffer sbuf = new StringBuffer(s);
 		SimpleDateFormat df = null;
 
-		if (s.length() > 19)
+		int slen = s.length();
+
+		if (slen > 19)
 		{
 			// The len of the ISO string to the second value is 19 chars. If
 			// greater then 19, there should be tz info and perhaps fractional
@@ -534,7 +536,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 					if (i < 24)
 						sbuf.append(c);
 					c = s.charAt(i++);
-				} while (Character.isDigit(c));
+				} while (i < slen && Character.isDigit(c));
 
 				// If there wasn't at least 3 digits we should add some zeros
 				// to make up the 3 digits we tell java to expect.
@@ -547,21 +549,28 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 				sbuf.append(".000");
 			}
 
-			// prepend the GMT part and then add the remaining bit of
-			// the string.
-			sbuf.append(" GMT");
-			sbuf.append(c);
-			sbuf.append(s.substring(i, s.length()));
-
-			// Lastly, if the tz part doesn't specify the :MM part then
-			// we add ":00" for java.
-			if (s.length() - i < 5)
-				sbuf.append(":00");
-
-			// we'll use this dateformat string to parse the result.
-			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+			if (i < slen)
+			{
+				// prepend the GMT part and then add the remaining bit of
+				// the string.
+				sbuf.append(" GMT");
+				sbuf.append(c);
+				sbuf.append(s.substring(i, slen));
+
+				// Lastly, if the tz part doesn't specify the :MM part then
+				// we add ":00" for java.
+				if (slen - i < 5)
+					sbuf.append(":00");
+
+				// we'll use this dateformat string to parse the result.
+				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+			}
+			else
+			{
+				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+			}
 		}
-		else if (s.length() == 19)
+		else if (slen == 19)
 		{
 			// No tz or fractional second info. 
 			// I'm not sure if it is
diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
index 58773b819baf1ad0a0994796fb261065efaea6aa..a983284e5cc2a783ae1316727addaab8df15059d 100644
--- a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
+++ b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
@@ -1630,11 +1630,12 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 
 			// Copy s into sbuf for parsing.
 			resultSet.sbuf.append(s);
+			int slen = s.length();
 
-			if (s.length() > 19)
+			if (slen > 19)
 			{
 				// The len of the ISO string to the second value is 19 chars. If
-				// greater then 19, there should be tz info and perhaps fractional
+				// greater then 19, there may be tz info and perhaps fractional
 				// second info which we need to change to java to read it.
 
 				// cut the copy to second value "2001-12-07 16:29:22"
@@ -1651,7 +1652,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 						if (i < 24)
 							resultSet.sbuf.append(c);
 						c = s.charAt(i++);
-					} while (Character.isDigit(c));
+					} while (i < slen && Character.isDigit(c));
 
 					// If there wasn't at least 3 digits we should add some zeros
 					// to make up the 3 digits we tell java to expect.
@@ -1664,21 +1665,29 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 					resultSet.sbuf.append(".000");
 				}
 
-				// prepend the GMT part and then add the remaining bit of
-				// the string.
-				resultSet.sbuf.append(" GMT");
-				resultSet.sbuf.append(c);
-				resultSet.sbuf.append(s.substring(i, s.length()));
-
-				// Lastly, if the tz part doesn't specify the :MM part then
-				// we add ":00" for java.
-				if (s.length() - i < 5)
-					resultSet.sbuf.append(":00");
-
-				// we'll use this dateformat string to parse the result.
-				df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+				if (i < slen)
+				{
+					// prepend the GMT part and then add the remaining bit of
+					// the string.
+					resultSet.sbuf.append(" GMT");
+					resultSet.sbuf.append(c);
+					resultSet.sbuf.append(s.substring(i, slen));
+
+					// Lastly, if the tz part doesn't specify the :MM part then
+					// we add ":00" for java.
+					if (slen - i < 5)
+						resultSet.sbuf.append(":00");
+
+					// we'll use this dateformat string to parse the result.
+					df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+				}
+				else
+				{
+					// Just found fractional seconds but no timezone.
+					df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+				}
 			}
-			else if (s.length() == 19)
+			else if (slen == 19)
 			{
 				// No tz or fractional second info. 
 				// I'm not sure if it is