开发者社区 问答 正文

获取当前时间时抛异常

使用下面代码获取当前时间:
screenshot
创建了另一个类作为数据模型。
screenshot
但是在分配当期日期的时候报出错误,不知道原因在那儿?
请大侠帮忙。

展开
收起
蛮大人123 2016-02-15 16:15:58 2116 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    试用此方法:

    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;
        }
    }
    2019-07-17 18:43:24
    赞同 展开评论
问答地址: