sjivan / gwt-ext

Automatically exported from code.google.com/p/gwt-ext
0 stars 0 forks source link

Convert Java date format to PHP (DateField) #502

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What?

add a convertDateFormat to DateUtils

1. Put this class into you client package
Code:
public class DateUtils {
   private static String[][] conversionTable = new String[][] { 
    { "yyyy", "Y" },
    { "yy", "y" },
    { "MMMMM", "F" },
    { "MMMM", "F" },
    { "MMM", "M" },
    { "MM", "m" },
    { "EEEEEE", "l" },
    { "EEEEE", "l" },
    { "EEEE", "l" },
    { "EEE", "D" },
    { "dd", "d" },
    { "HH", "H" },
    { "mm", "i" },
    { "ss", "s" },
    { "hh", "h" },
    { "A", "a" },
    { "S", "u" } 
   };

   /**
    * Converts java date format (used in e.g. GridPanel) to PHP date format
    * (used in DateField)
    *
    * @param javaFormat
    * e.g. dd-MM-yyyy
    * @return e.g. d-m-y
    */
   public static String convertDateFormat(String javaFormat) {
      String result = javaFormat;
      for (int i = 0; i < conversionTable.length; i++) {
         result = result.replaceAll(conversionTable[i][0],
conversionTable[i][1]);
      }
      return result;
   }
}

2. Let's use it :)
Code:
      String javaDateFormat =
DateTimeFormat.getMediumDateFormat().getPattern();
      String phpDateFormat = DateUtils.convertDateFormat(javaDateFormat);
      DateField creationDateField = new DateField("Creation date",
phpDateFormat);

Please provide any additional information below.
see forum suggestion:
http://www.gwt-ext.com/forum/viewtopic.php?f=9&t=1262&p=5208#p5208

Original issue reported on code.google.com by nietz...@gmail.com on 20 Jul 2009 at 1:04

GoogleCodeExporter commented 8 years ago

Original comment by ricardoserathiuk@gmail.com on 4 Aug 2009 at 8:07

GoogleCodeExporter commented 8 years ago
This conversion routine doesn't handle a bunch of cases.  Like this date 
"4/1/08" in 
Java would be "M/d/yy" but in PHP would be "n/j/y".  Neither the month nor day 
are 
handled in this case.  Not only that, but just adding another mapping with 
{"d", "j"} 
won't fix it since that would convert the "dd" to either "jj" or "j" instead of 
"d" 
depending on where the new mapping was inserted.

Original comment by tocon...@gmail.com on 20 Oct 2009 at 7:45