java代码获取当前系统时间年月日时分秒
可以使用Java中的Date类和SimpleDateFormat类来获取当前系统时间年月日时分秒。
示例代码:
import java.util.Date;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
// 创建Date对象,表示当前时间
Date now = new Date();
// 创建SimpleDateFormat对象,设置日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 调用format方法将当前时间格式化为指定格式的字符串
String time = sdf.format(now);
// 输出当前时间字符串
System.out.println(time);
}
}
输出结果:
2021-07-27 16:02:38
在上述代码中,首先创建了一个Date对象表示当前时间,然后创建了一个SimpleDateFormat对象来设置日期格式,最后调用format方法将当前时间格式化为指定格式的字符串。最终输出当前时间字符串。
原文地址: https://www.cveoy.top/t/topic/bSsy 著作权归作者所有。请勿转载和采集!