且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-05 17:12:58

使用 DateFormat#format 方法:

Convert a Date to a String using DateFormat#format method:

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);

来自 http://www.kodejava.org/examples/86.html