JAVA中如何实现显示当前时间

   2025-02-15 3070
核心提示:在Java中,可以使用java.util.Date类和java.text.SimpleDateFormat类来实现显示当前时间。import java.util.Date;import java.te

在Java中,可以使用java.util.Date类和java.text.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");                // 使用SimpleDateFormat的format方法将Date对象格式化为字符串        String currentTime = sdf.format(now);                // 输出当前时间        System.out.println("当前时间:" + currentTime);    }}

运行上述代码,将会输出当前时间的字符串表示,格式为yyyy-MM-dd HH:mm:ss,例如:“2022-01-01 12:34:56”。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言