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

报告相同问题?

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码