在Java中,可以使用java.sql.Time类来表示时间。要获取当前时间,可以使用java.util.Date类和java.sql.Time类的相互转换。

以下是获取当前时间的示例代码:

import java.util.Date;
import java.sql.Time;

public class Main {
    public static void main(String[] args) {
        // 获取当前时间
        Date currentDate = new Date();
        
        // 转换为Time类型
        Time currentTime = new Time(currentDate.getTime());
        
        // 输出当前时间
        System.out.println("当前时间:" + currentTime);
    }
}

输出结果类似于:

当前时间:14:30:45

请注意,java.sql.Time类只表示时间部分,不包含日期信息。如果需要同时获取日期和时间,可以使用java.util.Date类或java.time.LocalDateTime

java获取Time类型的当前时间

原文地址: https://www.cveoy.top/t/topic/hOxL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录