简体   繁体   English

计算 MySQL 中两个字符串日期之间的小时差?

[英]Calculate the difference in hours between two String dates in MySQL?

I have two String columns in MySQL database.我在 MySQL 数据库中有两个 String 列。 Those two columns were populated from a Java program in following way:这两列是通过以下方式从 Java 程序填充的:

System.currentTimeMillis(); //first column
System.currentTimeMillis(); + someStringHours //second column; the String, someStringDays reprensents some number of days, let's say 5 hours in millis...

Which function in MySQL can be used to calculated the difference to get number of hours between these two columns? MySQL 中的哪个函数可用于计算差异以获取这两列之间的小时数?

You call them string dates but they are actually UNIX timestamps in milliseconds (also called Javascript timestamps).您将它们称为string dates但它们实际上是以毫秒为单位的 UNIX 时间戳(也称为 Javascript 时间戳)。 That's what System.currentTimeMillis() generates.这就是System.currentTimeMillis()生成的内容。 It's a Java long data item, and a MySQL BIGINT data item.它是一个 Java long数据项和一个 MySQL BIGINT数据项。 You can store it in a string.您可以将其存储在字符串中。 (You can store it that way if you must, but searching and sorting numbers stored as strings is an unreliable mess; beware!) (如果必须,您可以以这种方式存储它,但是搜索和排序存储为字符串的数字是不可靠的;当心!)

A typical Javascript timestamp (or UNIX timestamp in milliseconds) is a big integer like 1600858176374456 .典型的 Javascript 时间戳(或以毫秒为单位的 UNIX 时间戳)是一个大整数,如1600858176374456 1 600 858 176 374 456 . 1 600 858 176 374 456

You can convert such a timestamp to a MySQL TIMESTAMP value with FROM_UNIXTIME() like this您可以像这样使用FROM_UNIXTIME()将这样的时间戳转换为 MySQL TIMESTAMP 值

    FROM_UNIXTIME(column * 0.001)

Why multiply the column value by 0.001 (that is, divide it by 1000)?为什么要将列值乘以 0.001(即除以 1000)? Because FROM_UNIXTIME() takes the timestamp in seconds, whereas System.currentTmeMillis() generates it in milliseconds.因为 FROM_UNIXTIME() 以秒为单位获取时间戳,而System.currentTmeMillis()以毫秒为单位生成它。

Then you can use DATEDIFF() like this然后你可以像这样使用DATEDIFF()

    DATEDIFF(FROM_UNIXTIME(laterTs*0.001),FROM_UNIXTIME(earlierTs*0.001))

This gives an integer number of days.这给出了整数天。

If you need the time difference in some other unit, such as hours, minutes, or calendar quarters, you can use TIMESTAMPDIFF() .如果您需要其他单位的时差,例如小时、分钟或日历季度,您可以使用TIMESTAMPDIFF() This gives you your difference in hours.这给你你的时间差异。

TIMESTAMPDIFF(HOUR,
              FROM_UNIXTIME(laterTs*0.001),
              FROM_UNIXTIME(earlierTs*0.001));

You can use SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR as the time unit in this function.您可以使用SECONDMINUTEHOURDAYWEEKMONTHQUARTERYEAR作为此函数中的时间单位。 Pro tip : Use your DBMS's date arithmetic functions if you possibly can.专业提示:如果可能,请使用 DBMS 的日期算术函数。 They've worked out all sorts of edge cases for you already.他们已经为您解决了各种边缘情况。

And, by the way, if you declare your columns like this ( Timestamp with a millisecond precision: How to save them in MySQL ):而且,顺便说一句,如果您像这样声明您的列( 具有毫秒精度的时间戳:如何将它们保存在 MySQL 中):

laterTs    TIMESTAMP(3),
earlierTs  TIMESTAMP(3),

You'll have an easier time indexing on and searching by these times.您可以更轻松地索引和搜索这些时间。

SELECT (1600858176374-1600944576374)/(24*60*60*1000) as Days

其中 (1600858176374-1600944576374) 是时间戳, (24 60 60*1000) 是一天中的工厂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM