java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyyMMdd");
java.util.Date theDate = fmt.parse("20041221");

解决方案 »

  1.   

    楼上的方法返回的怎么是"Tue Dec 21 00:00:00 GMT+08:00 2004",
    我只要"20041221"Date型的,该怎么做啊?
    继续请教!
      

  2.   

    楼主真....学会自己学习,自己看api !!
    都告诉你 用 SimpleDateFormat 了.你自己查api,不就什么都知道了???
      

  3.   

    Generally Date is used for calculation,and String is used for display. 
    Since you can get the real Date object, you will have everything including 
    time and date. If you want to display it with a particular format, you have to format it again to be a String. For example:java.text.SimpleDateFormat fmt = new  java.text.SimpleDateFormat("yyyyMMdd");
    java.util.Date theDate = fmt.parse("20041221");fmt.applyPattern("yyyy-MM-dd");
    String desiredFormat = fmt.format(theDate);you will get the desiredFormat with value "2004-12-21";
      

  4.   

    不懂才问的啊,教我怎么查啊,我按照方法,返回的的确是"Tue Dec 21 00:00:00 GMT+08:00 2004",究竟哪里不对?
      

  5.   

    因为Date不是String,什么叫做“20041221”的Date嘛?
    你不可能去改变Date类的toString行为
    如果要让Date显示为“20041221”,就应该再用SimpleDateFormat的format方法
      

  6.   

    还是楼上的说的清楚,我终于明白了,原来还要用format转一下,靠,谢谢各位了!
      

  7.   

    设置SimpleDateFormat
    yyyyMMddHHmmss
    yyyy四位表示的年,如:1999
    MM表示月,如:12
    dd表示日期,如12号
    HH表示24小时年,如16
    mm表示分钟,如12
    ss表示秒,如12秒
      

  8.   

    yun.......................................