我用
Date dt=new Date();
String s=DateFormat.getDateInstance(DateFormat.DEFAULT).format(dt).replaceAll("-","");
转换出来的有问题,比如2005年12月2日,它显示2005122,我其实想要20051202.

解决方案 »

  1.   

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");System.out.println(sdf.format(new Date()));
      

  2.   

    public class DateUtilTools {

    /**
     * 
     * 方法名:       
     * 功能描述:    两个参数格式必须为20050827,而且不能为空。
     * 参数说明:   第一个参数小于第二个参数返回true
     * 返回值:      // 函数返回值的说明
     * 其他:        // 其它说明
     */
        public static boolean lessThan(String preDate, String date) {
         if(preDate == null && date == null){
         return false;
         }
         else if(preDate == null){
         return true;
         }
         else if(date == null){
         return false;
         }
         preDate = preDate.trim();
         date = date.trim();
         if(preDate.length() == 8 && date.length() == 8){
                Integer date1 = new Integer(preDate);
                Integer date2 = new Integer(date);
                if(date1.compareTo(date2) < 0){
                 return true;
                }
         }
            return false;
        }
    /**
     * 
     * 方法名:       
     * 功能描述:    两个参数格式必须为20050827,而且不能为空。
     * 参数说明:   第一个参数大于第二个参数返回true
     * 返回值:      // 函数返回值的说明
     * 其他:        // 其它说明
     */
        public static boolean greaterThan(String preDate, String date) {
         if(preDate == null && date == null){
         return false;
         }
         else if(preDate == null){
         return false;
         }
         else if(date == null){
         return true;
         }
         preDate = preDate.trim();
         date = date.trim();
         if(preDate.length() == 8 && date.length() == 8){
                Integer date1 = new Integer(preDate);
                Integer date2 = new Integer(date);
                if(date1.compareTo(date2) > 0){
                 return true;
                }
         }
            return false;
        }
    /**
     * 
     * 方法名:       
     * 功能描述:    两个参数格式必须为20050827,而且不能为空。
     * 参数说明:
     * 返回值:      // 函数返回值的说明
     * 其他:        // 其它说明
     */
        public static boolean equal(String preDate, String date) {
         if(preDate == null && date == null){
         return false;
         }
         else if(preDate == null){
         return false;
         }
         else if(date == null){
         return true;
         }
         preDate = preDate.trim();
         date = date.trim();
         if(preDate.length() == 8 && date.length() == 8){
                Integer date1 = new Integer(preDate);
                Integer date2 = new Integer(date);
                if(date1.compareTo(date2) == 0){
                 return true;
                }
         }
            return false;
        }
        
        /**
         * 
         * @param 19位的时间 yyyy-MM-dd HH:mm:ss
         * 
         * @return 15位的时间 yyyyMMdd HHmmss
         */
    public static String _time19To15(String time_19) {
        String time_15 = "";
    if( time_19 == null || "".equals(time_19) || time_19.length() != 19 ) {
    time_15 = "";
    }else {
        String[] r = time_19.replace('-','#').replace(':','#').split("#");
        for(int i=0;i<r.length;i++)
        {
         time_15 += r[i];
        }
    }
        return time_15;
    }    /**
         * 
         * 
         * @param 15位的时间 yyyyMMdd HHmmss
         * 
         * @return 19位的时间 yyyy-MM-dd HH:mm:ss
         */
    public static String _time15To19(String time_15) {
    String time_19 = "";
    if( time_15 == null || "".equals(time_15) || time_15.length() != 15 ) {
    time_19 = "";
    }else {
    String y = time_15.substring(0, 4);
    String m = time_15.substring(4, 6);
    String d = time_15.substring(6, 8);
    String h = time_15.substring(9, 11);
    String mi = time_15.substring(11, 13);
    String s = time_15.substring(13, 15);
        time_19 = y+"-"+m+"-"+d+" "+h+":"+mi+":"+s;
    }
        return time_19;
    }    /**
         * 
         * @param 16位的时间 yyyy-MM-dd HH:mm
         * 
         * @return 13位的时间 yyyyMMdd HHmm
         */
    public static String _time16To13(String time_16) {
        String time_13 = "";
    if( time_16 == null || "".equals(time_16) || time_16.length() != 16 ) {
    time_13 = "";
    }else {
        String[] r = time_16.replace('-','#').replace(':','#').split("#");
        for(int i=0;i<r.length;i++)
        {
         time_13 += r[i];
        }
    }
        return time_13;
    }    /**
         * 
         * 
         * @param 13位的时间 yyyyMMdd HHmm
         * 
         * @return 16位的时间 yyyy-MM-dd HH:mm
         */
    public static String _time13To16(String time_13) {
    String time_16 = "";
    if( time_13 == null || "".equals(time_13) || time_13.length() != 13 ) {
    time_16 = "";
    }else {
    String y = time_13.substring(0, 4);
    String m = time_13.substring(4, 6);
    String d = time_13.substring(6, 8);
    String h = time_13.substring(9, 11);
    String mi = time_13.substring(11, 13);
        time_16 = y+"-"+m+"-"+d+" "+h+":"+mi;
    }
        return time_16;
    }

        /**
         * 
         * 
         * @param 10位的日期 yyyy-MM-dd
         * 
         * @return 8位的日期 yyyyMMdd
         */
    public static String _date10To8(String date_10) {
    String date_8 = "";
    if( date_10 == null || "".equals(date_10) || date_10.length() != 10 ) {
    date_8 = "";
    }else {
        String[] r = date_10.split("-");
        for(int i=0;i<r.length;i++)
        {
         date_8 += r[i];
        }
    }
    return date_8;
    }
        /**
         * 
         * 
         * @param 8位的日期 yyyyMMdd
         * 
         * @return 10位的日期 yyyy-MM-dd
         */
    public static String _date8To10(String date_8) {
    String date_10 = "";
    if( date_8 == null || "".equals(date_8) || date_8.length() != 8 ) {
    date_10 = "";
    }else {
    String y = date_8.substring(0, 4);
    String m = date_8.substring(4, 6);
    String d = date_8.substring(6, 8);
    date_10 = y+"-"+m+"-"+d;
    }
    return date_10;
    }

        /**
         * 
         * 
         * @param 7位的日期 yyyy-MM
         * 
         * @return 6位的日期 yyyyMM
         */
    public static String _date7To6(String date_7) {
    String date_6 = "";
    if( date_7 == null || "".equals(date_7) || date_7.length() != 7 ) {
    date_6 = "";
    }else {
        String[] r = date_7.split("-");
        for(int i=0;i<r.length;i++)
        {
         date_6 += r[i];
        }
    }
    return date_6;
    }
        /**
         * 
         * 
         * @param 6位的日期 yyyyMM
         * 
         * @return 7位的日期 yyyy-MM
         */
    public static String _date6To7(String date_6) {
    String date_7 = "";
    if( date_6 == null || "".equals(date_6) || date_6.length() != 6 ) {
    date_7 = "";
    }else {
    String y = date_6.substring(0, 4);
    String m = date_6.substring(4, 6);
    date_7 = y+"-"+m;
    }
    return date_7;
    }
    }
      

  3.   

    java.sql.Date date = new java.sql.Date( new java.util.Date().getTime())这个date就是你要的那种格式了!
      

  4.   

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");System.out.println(sdf.format(new Date()));
      

  5.   

    DateFormat sdf = new SimpleDateFormat("yyyyMMdd",Locale.CHINA);
      

  6.   

    SimpleDateFormat oDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    再用oDateFormat.format()方法。
      

  7.   

    只要用SimpleDateFromat 设置一下日期输出格式就行了嘛。
    SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMdd);
    Date date = dateFormat.parse("2005-12-01");
    System.out.println(dateFormat.format(date));
      

  8.   

    SimpleDateFormat dateFormat1 = new SimpleDateFormat(yyyy-MM-dd);//设置解析格式
    SimpleDateFormat dateFormat2 = new SimpleDateFormat(yyyyMMdd);//设置输出格式
    Date date = dateFormat.parse("2005-12-01");
    System.out.println(dateFormat2.format(date));
      

  9.   

    假如你的格式是2005年12月2日
    要转为20051202形式
    你可以将
      SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy年MM月dd日");
      这样就可以将2005年12月2日字符串转化为日期型的了