用Timestamp 想取出 2007/12/28 09:32:31 这种类型的日期时间,
请各位大虾帮忙看一下!
Timestamp add_date =
        new Timestamp(System.currentTimeMillis());
这个怎么一直出错?

解决方案 »

  1.   

    public class TimeStaticMethod {

    /**
       * @see 得到当前时刻的时间字符串
       * @return String para的标准时间格式的串,例如:返回'2003-08-09 16:00:00'
       */
      public static String getLocalString() {
        java.util.Date currentDate = new java.util.Date();
        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(currentDate);     return date;
      }

    public static String getLocalString(java.util.Date currentDate) {
        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(currentDate);     return date;
      }
        
          // 的到默认的时间格式为("yyyy-MM-dd HH:mm:ss")的当前时间
      public static String getCurrentDateTime() {
        return getCurrentDateTime("yyyy-MM-dd HH:mm:ss");
      }
      
      /*
       根据输入的格式(String _dtFormat)得到当前时间格式
       */
      public static String getCurrentDateTime(String _dtFormat) {
        String currentdatetime = "";
        try {
          Date date = new Date(System.currentTimeMillis());
          SimpleDateFormat dtFormat = new SimpleDateFormat(_dtFormat);
          currentdatetime = dtFormat.format(date);
        }
        catch (Exception e) {
          System.out.println("时间格式不正确");
          e.printStackTrace();
        }
        return currentdatetime;
      }
      
      public static Timestamp getTimestamp(String str) {
        Timestamp ret = null;
        try {
          SimpleDateFormat dateFormat = new SimpleDateFormat(
              "yyyy-MM-dd HH:mm:ss");       Date date = dateFormat.parse(str);
          long datelong = date.getTime();
          ret = new Timestamp(datelong);     }
        catch (Exception e) {
        }
        return ret;
      }
      
      public static Timestamp getTimestamp(String str,String _dtFormat) {
        Timestamp ret = null;
        try {
          SimpleDateFormat dateFormat = new SimpleDateFormat(
           _dtFormat);       Date date = dateFormat.parse(str);
          long datelong = date.getTime();
          ret = new Timestamp(datelong);     }
        catch (Exception e) {
        }
        return ret;
      }
      
      public static Timestamp getTimestamp() {
      return getTimestamp(0);
      }   public static String getTimestampString(Timestamp sta) {
      String ret= "";
      try{
          String str = sta.toString();
          ret = str.substring(0, str.lastIndexOf('.'));
      }
      catch(Exception e){
          ret = "";
      }
      return ret;
      }
      
      /** length 推荐直接用 StaticVariable中的 YearToMonth 和 YearToDay 做参数.
      * YearToMonth: 2007-01
      * YearToDay: 2007-01-30
      * @param sta
      * @param length
      * @return
      */
    public static String getTimestampFormat(Timestamp sta, int length) {
      String ret= "";
      try{
          String str = sta.toString();
          ret = str.substring(0, length);
      }
      catch(Exception e){
          ret = "";
      }
      return ret;
      }
      
      public static Timestamp getTimestamp(int disday) {
      Calendar c;
      c = Calendar.getInstance();
      long realtime = c.getTimeInMillis();
      realtime += 86400000 * disday;
      return new Timestamp(realtime);
      }
      
      /**
       * @see 得到时间字符串
       * @see 例如:StaticMethod.getDateString(-1),可以返回昨天的时间字符串
       * @param disday int 和当前距离的天数
       * @return String para的标准时间格式的串,例如:返回'2003-8-10 00:00:00'
       */   public static String getTimeString(int disday) {
        String ls_display = "";
        Calendar c;
        c = Calendar.getInstance();
        long realtime = c.getTimeInMillis();
        realtime += 86400000 * disday;
        c.setTimeInMillis(realtime);
        String _yystr = "", _mmstr = "", _ddstr = "";
        int _yy = c.get(Calendar.YEAR);
        _yystr = _yy + "";
        int _mm = c.get(Calendar.MONTH) + 1;
        _mmstr = _mm + "";
        if (_mm < 10) {
          _mmstr = "0" + _mm;
        }
        int _dd = c.get(Calendar.DATE);
        _ddstr = _dd + "";
        if (_dd < 10) {
          _ddstr = "0" + _dd;
        }
        ls_display = "'" + _yy + "-" + _mm + "-" + _dd + " 00:00:00'";
        return ls_display;
      }
      
      public static Date getCurrentDateTime(String strDate, String _dtFormat) {
        Date tDate = null;
        
        SimpleDateFormat smpDateFormat = new SimpleDateFormat(_dtFormat);
        ParsePosition pos = new ParsePosition(0);
        tDate = smpDateFormat.parse(strDate, pos); //标准格式的date类型时间
        
        return tDate;
      }
      

  2.   

    过期方法(不推荐用)Timestamp ts = new Timestamp(2007, 12, 28, 9, 32, 31, 0);
    正常点的写法        String date = "2007/12/28 09:32:31";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Timestamp ts = new Timestamp(sdf.parse(date).getTime());
    取当前时间的写法        Date date = new Date();
            Timestamp ts = new Timestamp(date.getTime());
      

  3.   

    刚才机器出了点小问题,可能太累了 嘻嘻~
    这个代码--
    Timestamp add_date = 
            new Timestamp(System.currentTimeMillis());
     -- 
    一直出错,其实从新启动它正确取出了Timestamp 类型的当前时间,《2008/06/02 18:27:10》还是感谢各位大虾帮助~~~!!!