简体   繁体   中英

Convert Time variable to milliseconds

I am having a variable which is called from_time and I am reading the infor from the DB.

Time from_time = rs.getTime("nfrm_time");

How to convert the from_time into milliseconds and seconds?

Thanks for the help

java.sql.Time扩展了java.util.Date ,并且具有返回millis的getTime()方法。

You should use the method getTime():

public class Main
{
    public static void main(String[] args)
    {
        Time t = new Time(System.currentTimeMillis());
        long l = t.getTime();
    }
}

Once you have the millis as indicated by Bozho, you can use java.util.concurrent.TimeUnit to rather easily convert to seconds (and hours, days, etc).

Even though it's actually in the concurrent package, I found this to be of great use for general cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM