How to convert getTime in seconds?

Could you help in the matter:

I defined a variable that:

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

and he will read the values ​​7:15:00

How to convert this type to seconds?

+4
source share
6 answers

Call getTime to get the number of milliseconds since January 1, 1970. Divide by 1000 to get it in seconds:

 long unixTime = from_time.getTime() / 1000; 

To get the number of seconds from 00:00 of the current day, use

 Calendar c = Calendar(); c.setTime(from_time); long daySeconds = (c.get(Calendar.SECONDS) + c.get(Calendar.MINUTES) * 60 + c.get(Calendar.HOURS) * 3600); 
+9
source

long seconds = rs.getTime("nfrm_time").getTime() / 1000

Here is an explanation:

rs.getTime("nfrm_time") returns java.sql.Time , which is actually a subclass of java.util.Date .

java.util.Date.getTime() returns the time in milliseconds that we divide by 1000 to get the seconds.

Note:

If you are looking for duration,

 Calendar cal = Calendar.getInstance(); cal.setTime(rs.getTime("nfrm_time")); // set to the time returned by resultset cal.set(0, 0, 0); // reset the year, month and date fields Calendar cal2 = Calendar.getInstance(); cal2.set(0, 0, 0, 0, 0, 0); // reset all the fields, including time long duration = ((cal.getTimeInMillis() - cal2.getTimeInMillis()) / 1000) + 1; 
+3
source

Try:

 from_time.getTime() / 1000 

This might work with:

Date components must be set to the "zero era" of January 1, 1970 and should not be available.

This means that part of the date is always the day of the era, which means that the Time instance is represented by the number of milliseconds since the beginning of the day.

+1
source

java.sql.Time inherits from java.util.Date, which has a getTime () method that returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.

So from_time.getTime()/1000 should do the trick.

+1
source

A cleaner way Time from_time = Math.round(rs.getTime("nfrm_time")/1000);

0
source

To get milliseconds:

 long ms = from_time.getTime(); 
-1
source

All Articles