【问题标题】:java.sql.Timestamp is throwing errorjava.sql.Timestamp 抛出错误
【发布时间】:2013-03-14 06:51:45
【问题描述】:

我正在使用以下方法获取当前时间:

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
    }
}

不明白为什么在分配当前日期时会抛出错误

【问题讨论】:

  • 非常非常怀疑一个简单的赋值是否会引发异常。但是,您甚至没有告诉我们错误是什么,这让您很难为您提供帮助......(当您的数据明显不是完整的时间戳时,为什么要使用java.sql.Timestamp ,无论如何?如果你只有java.util.Date级别的数据,为什么不坚持呢?)
  • 我很惊讶您的方法getCurrentDateAndTime() 没有要求您throwcatch 某个ParseException。跨度>
  • @R.J 它有一个投掷和接球,但在这里发布之前我已经删除以缩短空间
  • 对不起各位!!!我的错。我还没有初始化对象。

标签: java android timestamp


【解决方案1】:
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;
    }
}

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2015-03-24
    • 2011-12-27
    • 2018-08-06
    相关资源
    最近更新 更多