首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将java.util.Date转换为字符串

将java.util.Date转换为字符串
EN

Stack Overflow用户
提问于 2011-04-16 08:56:56
回答 13查看 1.3M关注 0票数 434

我想在Java中将java.util.Date对象转换为String

格式为2010-05-30 22:15:52

EN

回答 13

Stack Overflow用户

发布于 2011-04-16 09:02:29

Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = formatter.format(date);
票数 244
EN

Stack Overflow用户

发布于 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);

这使用Formatterrelative indexing而不是SimpleDateFormat,后者是not thread-safe,顺便说一下。

重复性稍高一些,但只需要一条语句。在某些情况下,这可能很方便。

票数 22
EN

Stack Overflow用户

发布于 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
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5683728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档