james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r738880 - in /james/mime4j/trunk/src/main/java/org/apache/james/mime4j: field/Fields.java util/MimeUtil.java
Date Thu, 29 Jan 2009 15:05:47 GMT
Author: mwiederkehr
Date: Thu Jan 29 15:05:47 2009
New Revision: 738880

URL: http://svn.apache.org/viewvc?rev=738880&view=rev
Log:
MIME4J-100: moved code to format an RFC822 date string into MimeUtil

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java?rev=738880&r1=738879&r2=738880&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java Thu Jan 29
15:05:47 2009
@@ -19,14 +19,9 @@
 
 package org.apache.james.mime4j.field;
 
-import java.text.DateFormat;
-import java.text.FieldPosition;
-import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
-import java.util.GregorianCalendar;
-import java.util.Locale;
 import java.util.Map;
 import java.util.TimeZone;
 
@@ -67,14 +62,6 @@
                 Field.CONTENT_TRANSFER_ENCODING, contentTransferEncoding);
     }
 
-    public static DateTimeField date(String fieldValue) {
-        return date(Field.DATE, fieldValue);
-    }
-
-    public static DateTimeField date(String fieldName, String fieldValue) {
-        return parse(DateTimeField.class, fieldName, fieldValue);
-    }
-
     public static DateTimeField date(Date date) {
         return date(Field.DATE, date, null);
     }
@@ -84,11 +71,15 @@
     }
 
     public static DateTimeField date(String fieldName, Date date, TimeZone zone) {
-        DateFormat df = RFC822_DATE_FORMAT.get();
-        if (zone != null) {
-            df.setTimeZone(zone);
-        }
-        return date(fieldName, df.format(date));
+        return date(fieldName, MimeUtil.formatDate(date, zone));
+    }
+
+    public static DateTimeField date(String fieldValue) {
+        return date(Field.DATE, fieldValue);
+    }
+
+    public static DateTimeField date(String fieldName, String fieldValue) {
+        return parse(DateTimeField.class, fieldName, fieldValue);
     }
 
     public static Field messageId(String hostname) {
@@ -256,40 +247,4 @@
         return sb.toString();
     }
 
-    private static final ThreadLocal<DateFormat> RFC822_DATE_FORMAT = new ThreadLocal<DateFormat>()
{
-        @Override
-        protected DateFormat initialValue() {
-            return new Rfc822DateFormat();
-        }
-    };
-
-    private static final class Rfc822DateFormat extends SimpleDateFormat {
-        private static final long serialVersionUID = 1L;
-
-        public Rfc822DateFormat() {
-            super("EEE, d MMM yyyy HH:mm:ss ", Locale.US);
-        }
-
-        @Override
-        public StringBuffer format(Date date, StringBuffer toAppendTo,
-                FieldPosition pos) {
-            StringBuffer sb = super.format(date, toAppendTo, pos);
-
-            int zoneMillis = calendar.get(GregorianCalendar.ZONE_OFFSET);
-            int dstMillis = calendar.get(GregorianCalendar.DST_OFFSET);
-            int minutes = (zoneMillis + dstMillis) / 1000 / 60;
-
-            if (minutes < 0) {
-                sb.append('-');
-                minutes = -minutes;
-            } else {
-                sb.append('+');
-            }
-
-            sb.append(String.format("%02d%02d", minutes / 60, minutes % 60));
-
-            return sb;
-        }
-    }
-
 }

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java?rev=738880&r1=738879&r2=738880&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/MimeUtil.java Thu Jan 29
15:05:47 2009
@@ -19,13 +19,19 @@
 
 package org.apache.james.mime4j.util;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
+import java.text.DateFormat;
+import java.text.FieldPosition;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Random;
+import java.util.TimeZone;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * A utility class, which provides some MIME related application logic.
@@ -394,6 +400,24 @@
     }
 
     /**
+     * Formats the specified date into a RFC 822 date-time string.
+     * 
+     * @param date
+     *            date to be formatted into a string.
+     * @param zone
+     *            the time zone to use or <code>null</code> to use the default
+     *            time zone.
+     * @return the formatted time string.
+     */
+    public static String formatDate(Date date, TimeZone zone) {
+        DateFormat df = RFC822_DATE_FORMAT.get();
+        if (zone != null) {
+            df.setTimeZone(zone);
+        }
+        return df.format(date);
+    }
+
+    /**
      * Splits the specified string into a multiple-line representation with
      * lines no longer than 76 characters (because the line might contain
      * encoded words; see <a href='http://www.faqs.org/rfcs/rfc2047.html'>RFC
@@ -451,4 +475,40 @@
     private static synchronized int nextCounterValue() {
         return counter++;
     }
+
+    private static final ThreadLocal<DateFormat> RFC822_DATE_FORMAT = new ThreadLocal<DateFormat>()
{
+        @Override
+        protected DateFormat initialValue() {
+            return new Rfc822DateFormat();
+        }
+    };
+
+    private static final class Rfc822DateFormat extends SimpleDateFormat {
+        private static final long serialVersionUID = 1L;
+
+        public Rfc822DateFormat() {
+            super("EEE, d MMM yyyy HH:mm:ss ", Locale.US);
+        }
+
+        @Override
+        public StringBuffer format(Date date, StringBuffer toAppendTo,
+                FieldPosition pos) {
+            StringBuffer sb = super.format(date, toAppendTo, pos);
+
+            int zoneMillis = calendar.get(GregorianCalendar.ZONE_OFFSET);
+            int dstMillis = calendar.get(GregorianCalendar.DST_OFFSET);
+            int minutes = (zoneMillis + dstMillis) / 1000 / 60;
+
+            if (minutes < 0) {
+                sb.append('-');
+                minutes = -minutes;
+            } else {
+                sb.append('+');
+            }
+
+            sb.append(String.format("%02d%02d", minutes / 60, minutes % 60));
+
+            return sb;
+        }
+    }
 }



Mime
View raw message