import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStringExample {
public static void main(String[] args) {
// 创建一个Date对象,表示当前时间
Date currentDate = new Date();
// 定义日期格式,例如:yyyy-MM-dd HH:mm:ss
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将Date对象转换为字符串
String formattedDate = dateFormat.format(currentDate);
// 输出结果
System.out.println("当前时间: " + formattedDate);
}
}
Date
对象:我们使用 new Date()
来获取当前的时间。SimpleDateFormat
类来指定日期和时间的格式。这里我们使用了 "yyyy-MM-dd HH:mm:ss"
格式。format()
方法将 Date
对象转换为指定格式的字符串。如果你需要其他格式的日期字符串,只需修改 SimpleDateFormat
中的格式模板即可。
上一篇:java使用opencv
下一篇:java 日期格式问题
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站