java线程中时间戳报错及时间戳获取问题

报错:: Servlet.service() for servlet [action] in context with path
[/PandaStarOA] threw exception [java.lang.IllegalArgumentException:
Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]] with root
cause

java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]


GregorianCalendar gcal = new GregorianCalendar(); // 获得当前时间
 
// 设定格式yyyy-mm-dd hh:mm:ss
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
Date date = gcal.getTime(); // 将当前时间转成日期对象
 
String datetime = df.format(date); // 获得符合格式的字符串,当前日期时间
 
int index = datetime.indexOf(" ");
String selectday = datetime.substring(0, index); // 当前日期
 
// 将当前日期换成Timestamp对象
// string的类型必须形如: yyyy-mm-dd hh:mm:ss[.f...] 这样的格式,中括号表示可选,// 否则报错!!!
Timestamp nowdatetime = Timestamp.valueOf(datetime);

附带几种,获取java获取时间戳的方式:

1、long currentTime = System.currentTimeMillis();

2、long currentTime = new Date().getTime();

3、long currentTime = Calendar.getInstance().getTimeInMillis();

三种获取方式,获取速度:1>2>3

posted @ 2022-01-27 18:37  zhangdaopin  阅读(603)  评论(0编辑  收藏  举报