旅游网站建设现状,天津个人专业做网站,wordpress 拖动分类,点击颜色更换网站主题用Timestamp来记录日期时间还是很方便的#xff0c;但有时候显示的时候是不需要小数位后面的毫秒的#xff0c;这样就需要在转换为String时重新定义格式。 Date、String、Timestamp之间的转换#xff01; [java] view plaincopyprint? public static void main(String[] ar…用Timestamp来记录日期时间还是很方便的但有时候显示的时候是不需要小数位后面的毫秒的这样就需要在转换为String时重新定义格式。 Date、String、Timestamp之间的转换 [java] view plaincopyprint? public static void main(String[] args) { // TODO Auto-generated method stub DateFormat format new SimpleDateFormat(yyyy-MM-dd); Date date null; String str null; // String转Date str 2009-01-06; try { date format.parse(str); // Wed sep 26 00:00:00 CST 2007 } catch (ParseException e) { e.printStackTrace(); } date java.sql.Date.valueOf(str); // 只保留日期部分返回的是java.sql.Date 2007-9-26 System.out.println(date); // Date转String date new Date(); // Wed sep 26 18 17:14:01 CST 2007 str format.format(date); // 2007-9-26 System.out.println(str); format DateFormat.getDateInstance(DateFormat.SHORT); str format.format(date); // 07-9-26 System.out.println(str); format DateFormat.getDateInstance(DateFormat.MEDIUM); str format.format(date); // 2007-9-26 System.out.println(str); format DateFormat.getDateInstance(DateFormat.FULL); str format.format(date); // 2007年9月26日 星期三 System.out.println(str); } Timestamp和String之间转换的函数 [java] view plaincopyprint? public static void main(String[] args) { // TODO Auto-generated method stub //Timestamp转化为String: SimpleDateFormat df new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//定义格式不显示毫秒 Timestamp now new Timestamp(System.currentTimeMillis());//获取系统当前时间 String str df.format(now); System.out.println(str); ///String转化为Timestamp: SimpleDateFormat df1 new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); Date date new Date(); String time df1.format(date); Timestamp ts Timestamp.valueOf(time); System.out.println(ts); } http://blog.csdn.net/cheung1021/article/details/6444043转载于:https://www.cnblogs.com/cmblogs/p/4402921.html