gpt4 book ai didi

java - 如何从 unix 时间戳中提取小时数?

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:00 24 4
gpt4 key购买 nike

我正在使用 Spring JPA。并且需要从 begin_time BIGINT 字段中提取小时。值看起来像这样

1547654400000    
1547568000000
1547654400000

这是我试过的

 @Query("select extract(hour from (s.beginTime/1000)) from TestTable s") 
public List<Integer> testQuery() {
// implementation
}

但它始终返回 null。

Java 有一个方法 System.currentTimeMillis() 。因此,spring JPA 中必须有一种方法可以从 unix 时间戳中提取小时。

如何从unix时间戳中提取小时数

最佳答案

HOUR(FROM_UNIXTIME(1547654400000/1000)) --> 8

  1. 除以 1000 得到从毫秒到“unixtime”。
  2. 转换为时间格式。
  3. 提取您需要的部分。 (这也可以通过 MID() 或其他字符串操作来完成。)

注意:这将获取与任何时区相关联的“小时”。

也许这就是你想要的:

FLOOR(1547654400000/(1000*3600))%24,另一方面,给你 16,值中的“小时”。

这样想:除以“小时”以下的部分(毫秒和秒/小时),然后对每天的小时数 (24) 取模。

关于java - 如何从 unix 时间戳中提取小时数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54210123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com