程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-java.sql.Timestamp拋出異常

android-java.sql.Timestamp拋出異常

編輯:編程綜合問答
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
    }
}

但是在分配當期日期的時候報出錯誤,不知道原因在那兒?

請大俠幫忙。

最佳回答:


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;
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved