[OFBiz] SVN: r5013 - trunk/base/src/base/org/ofbiz/base/util

jonesde at svn.ofbiz.org jonesde at svn.ofbiz.org
Fri May 20 13:27:05 EDT 2005


Author: jonesde
Date: 2005-05-20 12:27:02 -0500 (Fri, 20 May 2005)
New Revision: 5013

Modified:
   trunk/base/src/base/org/ofbiz/base/util/UtilDateTime.java
Log:
Applied patch from Jacopo Cappellato for some date formatting improvements; Jira #OFBIZ-248

Modified: trunk/base/src/base/org/ofbiz/base/util/UtilDateTime.java
===================================================================
--- trunk/base/src/base/org/ofbiz/base/util/UtilDateTime.java	2005-05-20 16:50:08 UTC (rev 5012)
+++ trunk/base/src/base/org/ofbiz/base/util/UtilDateTime.java	2005-05-20 17:27:02 UTC (rev 5013)
@@ -149,7 +149,16 @@
      * @return String formatted for right now
      */
     public static String nowDateString() {
-        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
+        return nowDateString("yyyyMMddHHmmss");
+    }
+    
+    /**
+     * Return a string formatted as format
+     *
+     * @return String formatted for right now
+     */
+    public static String nowDateString(String format) {
+        SimpleDateFormat df = new SimpleDateFormat(format);
         return df.format(new Date());
     }
 
@@ -558,36 +567,34 @@
     }
 
     /**
-     * Makes a date String in the format MM/DD/YYYY from a Date
+     * Makes a date String in the given from a Date
      *
      * @param date The Date
-     * @return A date String in the format MM/DD/YYYY
+     * @return A date String in the given format
      */
-    public static String toDateString(java.util.Date date) {
+    public static String toDateString(java.util.Date date, String format) {
         if (date == null) return "";
+        SimpleDateFormat dateFormat = null;
+        if (format != null) {
+            dateFormat = new SimpleDateFormat(format);
+        } else {
+            dateFormat = new SimpleDateFormat();
+        }
+        
         Calendar calendar = Calendar.getInstance();
 
         calendar.setTime(date);
-        int month = calendar.get(Calendar.MONTH) + 1;
-        int day = calendar.get(Calendar.DAY_OF_MONTH);
-        int year = calendar.get(Calendar.YEAR);
-        String monthStr;
-        String dayStr;
-        String yearStr;
-
-        if (month < 10) {
-            monthStr = "0" + month;
-        } else {
-            monthStr = "" + month;
-        }
-        if (day < 10) {
-            dayStr = "0" + day;
-        } else {
-            dayStr = "" + day;
-        }
-        yearStr = "" + year;
-        return monthStr + "/" + dayStr + "/" + yearStr;
+        return dateFormat.format(date);
     }
+    /**
+     * Makes a date String in the format MM/DD/YYYY from a Date
+     *
+     * @param date The Date
+     * @return A date String in the format MM/DD/YYYY
+     */
+    public static String toDateString(java.util.Date date) {
+        return toDateString(date, "MM/dd/yyyy");
+    }
 
     /**
      * Makes a time String in the format HH:MM:SS from a Date. If the seconds are 0, then the output is in HH:MM.



More information about the Svn mailing list