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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决