在Java中,可以使用SimpleDateFormat类来格式化时间。以下是一个示例代码:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 创建一个SimpleDateFormat对象,指定日期时间的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 创建一个Date对象,表示当前时间
Date now = new Date();
// 使用SimpleDateFormat对象将Date对象格式化为指定格式的字符串
String formattedDate = sdf.format(now);
// 输出格式化后的日期时间字符串
System.out.println(formattedDate);
}
}
上述代码使用SimpleDateFormat类将当前时间格式化为yyyy-MM-dd HH:mm:ss的格式。运行代码,输出的结果类似于2020-09-01 12:34:56。你可以根据需要修改SimpleDateFormat的参数来自定义日期时间的格式。更多关于SimpleDateFormat的详细用法可以参考Java官方文档。
785

被折叠的 条评论
为什么被折叠?



