gpt4 book ai didi

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

转载 作者:bug小助手 更新时间:2023-10-28 10:36:03 26 4
gpt4 key购买 nike

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

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

最佳答案

使用 DateFormat#formatDate 转换为 String方法:

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

关于java - 将 java.util.Date 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5683728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com