[英]java.sql.Timestamp is throwing error
I am taking the current time using following method: 我正在使用以下方法获取当前时间:
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;
}
}
Now created another class as a data model: 现在创建另一个类作为数据模型:
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
}
}
Can't understand why it's throwing error during assign the current date 无法理解为什么在分配当前日期期间抛出错误
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.