---
title: "Timestamp和String的相互转换"
author: "lkf009"
date: "2013-09-07T15:02:56Z"
tags: []
categories: ["编程语言"]
slug: "timestamp-string"
draft: false
lastmod: "2013-09-07T15:02:56Z"
id: 1938433
url: "https://lkf009.iteye.com/blog/1938433"
---

用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);