一尘不染

将java.util.Date转换为String

java

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

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


阅读 369

收藏
2020-02-29

共1个答案

一尘不染

转换日期为字符串使用DateFormat#format方法:

String pattern = "MM/dd/yyyy HH:mm:ss";

// Create an instance of SimpleDateFormat used for formatting 
// the string representation of date according to the chosen pattern
DateFormat df = new SimpleDateFormat(pattern);

// Get the today date using Calendar object.
Date today = Calendar.getInstance().getTime();        
// Using DateFormat format method we can create a string 
// representation of a date with the defined format.
String todayAsString = df.format(today);

// Print the result!
System.out.println("Today is: " + todayAsString);
2020-02-29