哪位大哥有日期格式转化的函数,要求如下
假如该函数名为String fun(string date, string format)
date为日期:如2005-05-16
formate为格式 如yyyyMMdd
希望能返回 20050516,如格式换成MMddyyyy,则返回05162005

解决方案 »

  1.   

    java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date d = sdf.parse(date);
    java.text.SimpleDateFormat sdfNew = new SimpleDateFormat("MMddyyyy");
    return sdfNew.format(d);
      

  2.   

    DateFormat:
    format
    public abstract StringBuffer format(Date date,
                                        StringBuffer toAppendTo,
                                        FieldPosition fieldPosition)Formats a Date into a date/time string.
    Parameters:date - a Date to be formatted into a date/time string.toAppendTo - the string buffer for the returning date/time string.fieldPosition - keeps track of the position of the field
     within the returned string.
     On input: an alignment field,
     if desired. On output: the offsets of the alignment field. For
     example, given a time text "1996.07.10 AD at 15:08:56 PDT",
     if the given fieldPosition is DateFormat.YEAR_FIELD, the
     begin index and end index of fieldPosition will be set to
     0 and 4, respectively.
     Notice that if the same time field appears
     more than once in a pattern, the fieldPosition will be set for the first
     occurrence of that time field. For instance, formatting a Date to
     the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
     "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
     the begin index and end index of fieldPosition will be set to
     5 and 8, respectively, for the first occurrence of the timezone
     pattern character 'z'.
    Returns:the formatted date/time string.