Convert java.util.Date to String

前端 未结 18 2651
悲&欢浪女
悲&欢浪女 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 06:01

    Why don't you use Joda (org.joda.time.DateTime)? It's basically a one-liner.

    Date currentDate = GregorianCalendar.getInstance().getTime();
    String output = new DateTime( currentDate ).toString("yyyy-MM-dd HH:mm:ss");
    
    // output: 2014-11-14 14:05:09
    

提交回复
热议问题