如何从 unix 时间戳中提取小时数?
How to extract hour from unix timestamp?
我正在使用 Spring JPA。并且需要从 begin_time
BIGINT
字段中提取小时。值看起来像这样
1547654400000
1547568000000
1547654400000
这是我试过的
@Query("select extract(hour from (s.beginTime/1000)) from TestTable s")
public List<Integer> testQuery() {
// implementation
}
但它始终 returns 为空。
Java 有一个方法 System.currentTimeMillis() 。因此,spring JPA 中必须有一种方法可以从 unix 时间戳中提取小时。
如何从 unix 时间戳中提取小时
如果 beginTime 是 unix 时间戳,你试试 extract(hour from FROM_UNIXTIME(s.last_login) )
,
希望能帮到你
HOUR(FROM_UNIXTIME(1547654400000/1000))
--> 8
- 除以 1000 得到从毫秒到 "unixtime"。
- 转换为时间格式。
- 提取您需要的部分。 (这也可以通过
MID()
或其他字符串操作来完成。)
注意:这将获得与任何时区关联的 "hour" 关联到值中。
也许这就是你想要的:
FLOOR(1547654400000/(1000*3600))%24
,另一方面,给你 16
,"hour" 中的值。
这样想:除以下面的部分 "hour"(毫秒和每小时秒数),然后对每天的小时数 (24) 取模。
我正在使用 Spring JPA。并且需要从 begin_time
BIGINT
字段中提取小时。值看起来像这样
1547654400000
1547568000000
1547654400000
这是我试过的
@Query("select extract(hour from (s.beginTime/1000)) from TestTable s")
public List<Integer> testQuery() {
// implementation
}
但它始终 returns 为空。
Java 有一个方法 System.currentTimeMillis() 。因此,spring JPA 中必须有一种方法可以从 unix 时间戳中提取小时。
如何从 unix 时间戳中提取小时
如果 beginTime 是 unix 时间戳,你试试 extract(hour from FROM_UNIXTIME(s.last_login) )
,
希望能帮到你
HOUR(FROM_UNIXTIME(1547654400000/1000))
--> 8
- 除以 1000 得到从毫秒到 "unixtime"。
- 转换为时间格式。
- 提取您需要的部分。 (这也可以通过
MID()
或其他字符串操作来完成。)
注意:这将获得与任何时区关联的 "hour" 关联到值中。
也许这就是你想要的:
FLOOR(1547654400000/(1000*3600))%24
,另一方面,给你 16
,"hour" 中的值。
这样想:除以下面的部分 "hour"(毫秒和每小时秒数),然后对每天的小时数 (24) 取模。