java怎么显示当前时间

2024-04-23

在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 currentDate = new Date();
        
        // 创建SimpleDateFormat对象来定义时间的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        // 使用SimpleDateFormat对象格式化当前时间并输出
        String currentTime = sdf.format(currentDate);
        System.out.println("当前时间是: " + currentTime);
    }
}

运行以上代码,将会输出当前时间的字符串表示,格式为yyyy-MM-dd HH:mm:ss,例如2020-05-05 12:30:45

《java怎么显示当前时间.doc》

下载本文的Word格式文档,以方便收藏与打印。