本文主要是介绍Timestamp 和String之间的转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用Timestamp来记录日期时间还是很方便的,但有时候显示的时候是不需要小数位后面的毫秒的,这样就需要在转换为String时重新定义格式。
Timestamp转化为String:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
String str = df.format(now);
String转化为Timestamp:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
Timestamp ts = Timestamp.valueOf(time);
这篇关于Timestamp 和String之间的转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!