类似这种格式的字符串“08  1 2009 12:00AM”如何格式化为 1字符串类型"2009 08 01" 2Date类型"2009 08 01" 

解决方案 »

  1.   

    如果是字符串 用repalce()最方便
    Data类型就用
    Calendar cal = Calendar.getInstance(); 
    java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
    String cdate = sdf.format(cal.getTime()); 
    System.out.println("times is :"+cdate); 
      

  2.   

    /**
     * 获取当前日期信息
     * 
     * @param value
     * @return
     */ public static String GetCurrentDateTime(int value) {
    String dateTime = "";
    int Year = 0;
    int Month = 0;
    int Day = 0;
    int Hour = 0;
    int Minute = 0;
    int Second = 0;
    Calendar cl = Calendar.getInstance(TimeZone.getDefault());
    Year = cl.get(Calendar.YEAR);
    Month = cl.get(Calendar.MONTH) + 1;
    Day = cl.get(Calendar.DATE);
    Hour = cl.get(Calendar.HOUR_OF_DAY);
    Minute = cl.get(Calendar.MINUTE);
    Second = cl.get(Calendar.SECOND);         dateTime = String.valueOf(Year) + "-" + String.valueOf(Month) + "-"
    + String.valueOf(Day);
    return dateTime;
    }
      

  3.   


    SimpleDateFormat sourceFormat = new SimpleDateFormat("MM dd yyyy hh:mmaa",
    new DateFormatSymbols(Locale.US));
    SimpleDateFormat destFormat=new SimpleDateFormat("yyyy MM dd");
    Date sourceDate = sourceFormat.parse("08 1 2009 12:00AM");
    String strTime=destFormat.format(sourceDate);
    System.out.println(strTime);
      

  4.   

      Date date=new Date();
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy MM dd ");
      sdf.format(date);
      out.println("当前时间为:"+ sdf.format(date) );
      

  5.   


    import java.text.SimpleDateFormat;
    import java.util.Date;public class DataFormatDemo {  public static void main(String[] args) throws Exception {
        String str = "08 1 2009 3:00PM";
        SimpleDateFormat format = new SimpleDateFormat("MM d yyyy hh:mma");
        Date time = format.parse(str);
        format.applyPattern("yyyy MM dd");
        System.out.println(format.format(time));  }
    }
      

  6.   

    上面

    format.format(time));就是字符串类型的。

    Date好像没有类型吧,只有输出的时候指定格式啊。

    Date time = format.parse(str);

    得到的就是了。