Convert java.util.Date to String

前端 未结 18 2564
悲&欢浪女
悲&欢浪女 2020-11-22 05:37

I want to convert a java.util.Date object to a String in Java.

The format is 2010-05-30 22:15:52

18条回答
  •  庸人自扰
    2020-11-22 05:46

    Altenative one-liners in plain-old 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-%

    This uses Formatter and relative indexing instead of SimpleDateFormat which is not thread-safe, btw.

    Slightly more repetitive but needs just one statement. This may be handy in some cases.

提交回复
热议问题