2 sachinks SachinKS 于 2013.03.14 16:20 提问

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
Kakalapa1986   2013.03.15 10: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;
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!