Convert java.util.Date to String

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

    In single shot ;)

    To get the Date

    String date = new SimpleDateFormat("yyyy-MM-dd",   Locale.getDefault()).format(new Date());
    

    To get the Time

    String time = new SimpleDateFormat("hh:mm", Locale.getDefault()).format(new Date());
    

    To get the date and time

    String dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefaut()).format(new Date());
    

    Happy coding :)

提交回复
热议问题