简体   繁体   中英

How to extract hour from unix timestamp?

I'm using Spring JPA. And need to extract hour from begin_time BIGINT field . Values look like this

1547654400000    
1547568000000
1547654400000   

Here is what I tried

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

But it always returns null.

Java has a method System.currentTimeMillis() . Therefore there must be a method within spring JPA to extract hour from unix timestamp.

How to extract hour from unix timestamp

如果beginTime是Unix时间戳,请尝试extract(hour from FROM_UNIXTIME(s.last_login) ) ,希望我能为您提供帮助。

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

  1. Divide by 1000 to get from milliseconds, to "unixtime".
  2. Convert to a time format.
  3. Extract the part you need. (This could be done with MID() or other string manipulation, too.)

Caution: This will get the "hour" associated with whatever timezone is baked into the value.

Perhaps this is what you wanted:

FLOOR(1547654400000/(1000*3600))%24 , on the other hand, gives you 16 , the "hour" that is in the value.

Think of it this way: Divide by the parts below "hour" (milliseconds and seconds-per-hour), then go modulo the number of hours per day (24).

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