使用:DateFormat df = DateFormat.getDateTimeInstance();
logger.error("df:" +df.format(StartTransTime));得到的结果是:2006-5-13 10:30:46,
但是我需要的是:2006-05-13 10:30:46,即如果只有一位数字,希望在前补一个0,
格式串是:yyyy-mm-dd hh:mm:ss   如果只有一位数字,希望在前补一个0.
请大家给一个现成例子!

解决方案 »

  1.   


      /**
       * 字符串转换成日期(date)
       * @param str
       * @return
       * @throws Exception
       */
      public static Calendar strToCalendar ( String str ) throws Exception {
      Date d = new Date();
      try{
              d = formatter.parse(str);
          }catch(Exception e){
              throw(e);
          }
          Calendar c = Calendar.getInstance();
          c.setTime( d );
          return c;
      }
      
      /**
       * Calendar转换成"yyyy-MM-dd HH:mm:ss"
       * @param c
       * @return
       * @throws Exception
       */
      public static String clendarToStr ( Calendar c ) throws Exception {
      String str = "";
      Date d = c.getTime();
      try {
      str = formatter.format( d );
      } catch ( Exception ex ) {
      throw (ex);
      }
      return str;
      }  public static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      

  2.   

    测试    Calendar c = Calendar.getInstance();//取当前系统时间
        try {
         String s = clendarToStr(c);
         System.out.println(s);
        } catch (Exception ex ) {
        
        }
    输出:2006-05-18 11:03:30
      

  3.   

    我要从一个java.util.Date  转换成 yyyy-mm-dd hh:mm:ss ,而不是Calendar ,该怎样写。
    谢谢各位弟兄!
      

  4.   


        /**
         * 字符串转化成日期类型
         * @param _Date
         * @return
         * @throws Exception
         */
        public static Date StringToDate(String _Date) throws Exception{
            try{
                return m_sdf.parse(_Date);
            }catch(Exception e){
                throw(e);
            }    
        } /**
     * 格式化日期
     * @param pattern 日期格式
     * @param date 日期对象
     * @return
     */
    public static String dateFormat(String pattern, java.util.Date date) {
        SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern);
        return simpledateformat.format(date);
    }
      

  5.   

    yyyy-MM-dd HH:mm:ss用这个吧
    yyyy-mm-dd hh:mm:ss