import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
// 创建一个Date对象,表示当前时间
Date currentDate = new Date();
// 定义日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将Date对象格式化为字符串
String formattedDate = dateFormat.format(currentDate);
// 输出格式化后的日期
System.out.println("当前时间: " + formattedDate);
}
}
java.text.SimpleDateFormat
和java.util.Date
。Date currentDate = new Date();
创建了一个表示当前时间的Date
对象。SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
使用SimpleDateFormat
类来定义日期的格式。这里的格式字符串"yyyy-MM-dd HH:mm:ss"
表示年-月-日 时:分:秒。String formattedDate = dateFormat.format(currentDate);
将Date
对象转换为指定格式的字符串。System.out.println
输出格式化后的日期。这段代码展示了如何使用Java中的SimpleDateFormat
类来格式化日期。
下一篇:linux 查看java版本
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站