我需要通过接口把时间查出来,然后set到Calendar类型的字段里面。取出来的格式是:yyyy-MM-dd的,要转成yyyyMMdd的存在Calendar类型的字段里。 private void testDate() {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse("2011-1-1"));
System.out.println(calendar.getTime());
System.out.println(simpleDateFormat.format(calendar.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
}为什么打印出来的是:
Sat Oct 30 00:00:00 CST 2010
20101030怎么不是2011-1-1啊?请教了。

解决方案 »

  1.   

    calendar.setTime(simpleDateFormat.parse("20110101"));
    试试
      

  2.   

    你日期格式 设为 yyyyMMdd (DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");)
    送进去的日期却是,2011-1-1 当然是错的。
      

  3.   


        private void testDate() {
            DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(simpleDateFormat.parse("2011-1-1"));
                System.out.println(calendar.getTime());
                System.out.println(simpleDateFormat.format(calendar.getTime(),"yyyyMMdd"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      

  4.   


        private static void testDate() {
            DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(simpleDateFormat.parse("2011-1-1"));
                System.out.println(calendar.getTime());
                System.out.println(simpleDateFormat.format(calendar.getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }1st  System.out.println(calendar.getTime());
    这样调用的时候会调用Calendar的toString方法:     public String toString() {
    // NOTE: BuddhistCalendar.toString() interprets the string
    // produced by this method so that the Gregorian year number
    // is substituted by its B.E. year value. It relies on
    // "...,YEAR=<year>,..." or "...,YEAR=?,...".
    StringBuilder buffer = new StringBuilder(800);
    buffer.append(getClass().getName()).append('[');
    appendValue(buffer, "time", isTimeSet, time);
    buffer.append(",areFieldsSet=").append(areFieldsSet);
    buffer.append(",areAllFieldsSet=").append(areAllFieldsSet);
    buffer.append(",lenient=").append(lenient);
    buffer.append(",zone=").append(zone);
    appendValue(buffer, ",firstDayOfWeek", true, (long) firstDayOfWeek);
    appendValue(buffer, ",minimalDaysInFirstWeek", true, (long) minimalDaysInFirstWeek);
    for (int i = 0; i < FIELD_COUNT; ++i) {
    buffer.append(',');
    appendValue(buffer, FIELD_NAME[i], isSet(i), (long) fields[i]);
    }
    buffer.append(']');
    return buffer.toString();
    } 该方法返回的区时Timezone格式的字符串。
      

  5.   


    private void testDate() {
    DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");    
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(simpleDateFormat.parse("2011-1-1"));
                System.out.println(calendar.getTime());
                simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
                System.out.println(simpleDateFormat.format(calendar.getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
      

  6.   

    楼主马虎问题,DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");yyyyMMdd改为yyyy-MM-dd就行了,请给分
      

  7.   

    格式化问题:
    DateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");yyyyMMdd改为yyyy-MM-dd就行
      

  8.   

    格式化个日期真不需要那么难的
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat (yyyy-MM-dd);
    sdf.format(date);
    System.out.println(sdf.format(date););
      

  9.   

    其实Calendar这个对象挺难控制的,比如:
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    calendar.setTime(format.parse("2011-02-29"));他会把2011-2-29解析成2011-3-1,需要,dateFormat.setLenient(false);
    这样再执行calendar.setTime(format.parse("2011-02-29"));的时候就会抛错了。