如何将 getTime 转换为秒?

发布于 2024-11-28 23:24:03 字数 150 浏览 0 评论 0原文

您能帮忙解决一下问题吗:

我定义了一个变量,它是:

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

它将读取值 7:15:00

如何将此类型转换为秒?

Can you please help in matter:

I have defined a variable which is:

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

and it will read the values 7:15:00

How to convert this type to seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

友谊不毕业 2024-12-05 23:24:03

调用 getTime 获取自 1970 年 1 月 1 日以来的毫秒数。除以 1000 即可获取秒数:

long unixTime = from_time.getTime() / 1000;

要获取当前日期 00:00 以来的秒数,请使用

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

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 since 00:00 of the current day, use the

Calendar c = Calendar();
c.setTime(from_time);
long daySeconds = (c.get(Calendar.SECONDS) +
                   c.get(Calendar.MINUTES) * 60 +
                   c.get(Calendar.HOURS) * 3600);
深空失忆 2024-12-05 23:24:03

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

解释如下:

rs.getTime("nfrm_time") 返回 java.sql .Time 实际上是 java.util.Date 的子类。

java.util.Date.getTime() 返回以毫秒为单位的时间,我们将其除以 1000 以获得秒数。

注意

如果您要查找持续时间,

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;

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

Here's the explanation:

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

java.util.Date.getTime() returns time in milli seconds which we divide by 1000 to get seconds.

Note:

If you're looking for duration instead,

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;
沒落の蓅哖 2024-12-05 23:24:03

尝试:

from_time.getTime() / 1000

这可能会起作用,因为:

日期组件应设置为 1970 年 1 月 1 日的“零纪元”值,并且不应被访问。

这意味着日期部分始终是纪元日,这意味着Time实例由自一天开始以来的毫秒数表示。

Try:

from_time.getTime() / 1000

This might work since:

The date components should be set to the "zero epoch" value of January 1, 1970 and should not be accessed.

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

时光是把杀猪刀 2024-12-05 23:24:03

java.sql.Time 继承自 java.util.Date,它有一个 getTime() 方法,该方法返回自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数。

所以 from_time.getTime()/1000 应该可以解决问题。

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

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

素染倾城色 2024-12-05 23:24:03

更清洁的方法是
时间 from_time = Math.round(rs.getTime("nfrm_time")/1000);

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

留一抹残留的笑 2024-12-05 23:24:03

要获取毫秒:

long ms = from_time.getTime();

To get the milliseconds:

long ms = from_time.getTime();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文