java.sql.Timestamp 抛出错误

标签 java android 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;
    }
}

关于java.sql.Timestamp 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15402944/

相关文章:

java - JNA在Win7 64位机器上使用32位dll

android - 如何在 android studio 中实现 JniLibs

java - Android - 一段时间后显示和隐藏文本?

php - 获取给定时间戳之前午夜的时间戳

php - 从mysql获取两个时间戳之间的数据

java - 获取 JavaScript JSON 字符串以填充 DataTable

java - 从另一个 Activity 设置 ImageView 资源和 TextView 文本

sql - ORA-01810 : format code appears twice

java - 在 mongodb 数组元素中设置值

android - 自定义对话框背景图片在android中显示不清晰