我想在Java中将java.util.Date
对象转换为String
。
格式为2010-05-30 22:15:52
发布于 2011-04-16 09:02:29
Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = formatter.format(date);
发布于 2015-06-26 01:41:20
简单老式java中的可变式一行程序:
String.format("The date: %tY-%tm-%td", date, date, date);
String.format("The date: %1$tY-%1$tm-%1$td", date);
String.format("Time with tz: %tY-%<tm-%<td %<tH:%<tM:%<tS.%<tL%<tz", date);
String.format("The date and time in ISO format: %tF %<tT", date);
这使用Formatter和relative indexing而不是SimpleDateFormat
,后者是not thread-safe,顺便说一下。
重复性稍高一些,但只需要一条语句。在某些情况下,这可能很方便。
发布于 2014-11-14 21:14:33
为什么不使用Joda (org.joda.time.DateTime)?这基本上是一行代码。
Date currentDate = GregorianCalendar.getInstance().getTime();
String output = new DateTime( currentDate ).toString("yyyy-MM-dd HH:mm:ss");
// output: 2014-11-14 14:05:09
https://stackoverflow.com/questions/5683728
复制相似问题