SachinKS 2013-03-14 08:20 采纳率: 0%
浏览 2502
已采纳

java.sql.Timestamp抛出异常

使用下面代码获取当前时间:

import java.sql.Timestamp;
public class TimeFormat
{
        public static Timestamp getCurrentDateAndTime()
        {
            String strFormat = new String("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat formatter = new SimpleDateFormat(strFormat);
            java.util.Date theDate = new java.util.Date();
            theDate = (java.util.Date) formatter.parse(formatter.format(theDate));
            Timestamp rtnTS = new Timestamp(theDate.getTime());
            return rtnTS;
        }
}

创建了另一个类作为数据模型。

public class InvoiceObject extends java.lang.Object implements Serializable
{
    public Integer mId;
    public Timestamp mTimeIssued;

    public InvoiceObject()
    {
            this.mId = new Long("0");
            Timestamp tempTime = TimeFormat.getCurrentDateAndTime(); //successful
        this.mTimeIssued = tempTime; //here throwing error
    }
}

但是在分配当期日期的时候报出错误,不知道原因在那儿?

请大侠帮忙。

  • 写回答

1条回答

  • Kakalapa1986 2013-03-15 02:04
    关注
    mport java.io.Serializable;
    import java.sql.Timestamp;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    public class TT {
    
        /**
         * @param args
         * @throws Exception 
         * @throws Exception 
         */
        public static Timestamp getCurrentDateAndTime() throws Exception
        {
            String strFormat = new String("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat formatter = new SimpleDateFormat(strFormat);
            java.util.Date theDate = new java.util.Date();
            theDate = (java.util.Date) formatter.parse(formatter.format(theDate));
            Timestamp rtnTS = new Timestamp(theDate.getTime());
            return rtnTS;
        }
        public static void main(String[] args) throws Exception {
    
            InvoiceObject i=new InvoiceObject();
            System.out.println(i.getmTimeIssued());
            }
    
    }
    class InvoiceObject extends java.lang.Object implements Serializable
    {
        public Integer mId;
        public Timestamp mTimeIssued;
    
        public InvoiceObject()
        {
               // this.mId = new Long("0");
                Timestamp tempTime;
                try {
                    tempTime = TT.getCurrentDateAndTime();
                      this.mTimeIssued = tempTime;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //successful
           //here throwing error
        }
        public Timestamp getmTimeIssued() {
            return mTimeIssued;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料