java 字符串<=======>时间戳 相互转换

来源:互联网 发布:编程员招聘 编辑:程序博客网 时间:2024/06/05 11:29
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class TestTime {public static void main(String[] args) {String time = "2010年12月08日11时17分00秒";System.out.println(time);// 字符串=======>时间戳String re_str = getTime(time);System.out.println(re_str);// 时间戳======>字符串String data = getStrTime(re_str);System.out.println(data);}// 将字符串转为时间戳public static String getTime(String user_time) {String re_time = null;SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");Date d;try {d = sdf.parse(user_time);long l = d.getTime();String str = String.valueOf(l);re_time = str.substring(0, 10);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return re_time;}// 将时间戳转为字符串public static String getStrTime(String cc_time) {String re_StrTime = null;SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");// 例如:cc_time=1291778220long lcc_time = Long.valueOf(cc_time);re_StrTime = sdf.format(new Date(lcc_time * 1000L));return re_StrTime;}}