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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵