英文:
UnixTime on spark / java
问题
我有以下数据框:
+------------------------+
| Tracking_Time |
+------------------------+
|2020-02-18 14:50:07 |
|2020-02-17 17:15:45 |
+------------------------+
我使用以下代码:
df.withColumn("current_date", expr("reflect('java.lang.System', 'currentTimeMillis')"))
.withColumn("Tracking_Time_ms", unix_timestamp(col("Tracking_Time")));
我得到以下输出
+------------------------+------------------------+------------------------+
| Tracking_Time |current_date |Tracking_Time_ms |
+------------------------+------------------------+------------------------+
|2020-02-18 14:50:07 |1598108268389 |1582037407 |
|2020-02-17 17:15:45 |1598108270182 |1581959745 |
+------------------------+------------------------+------------------------+
如何将“Tracking_Time_ms”的格式转换为“current_date”的格式?
英文:
I have the following dataframe :
+------------------------+
| Tracking_Time |
+------------------------+
|2020-02-18 14:50:07 |
|2020-02-17 17:15:45 |
+------------------------+
I use the following code :
df.withColumn("current_date", expr("reflect('java.lang.System', 'currentTimeMillis')"))
.withColumn("Tracking_Time_ms", unix_timestamp(col("Tracking_Time")));
I get the following output
+------------------------+------------------------+------------------------+
| Tracking_Time |current_date |Tracking_Time_ms |
+------------------------+------------------------+------------------------+
|2020-02-18 14:50:07 |1598108268389 |1582037407 |
|2020-02-17 17:15:45 |1598108270182 |1581959745 |
+------------------------+------------------------+------------------------+
How can I convert the format of Tracking_Time_ms
to the format of current_date
?
答案1
得分: 1
Unix时间戳是秒数的累加。此计数从1970年1月1日的Unix纪元(UTC时间)开始。
因此,您在Tracking_Time_ms下看到的值实际上是以秒为单位的。
要将其转换为毫秒(与current_date相同),您需要将其乘以1000。
英文:
The unix time stamp is a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC.
So the values you see under Tracking_Time_ms are actually in seconds.
To convert it to ms (to be the same as current_date) you need to multiply it by 1000.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论