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 @   zhangdaopin  阅读(638)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示