将奇怪的时间戳格式转换为正常格式?

问题描述

2021-05-06T02:21:20.000000Z

所以我得到了这种 ISO 8601 格式的时间戳,但它在中间而不是空格中有那个 T。不知道如何在 sql select 语句中编写脚本以将其转换为普通时间戳 Numberic:Continuous 格式。

选择 时间戳,from_unixtime(时间戳)

解决方法

我假设您正在使用 MySQL,因为您引用了 public class Main { public static void main(String args[]){ try{ FileOutputStream fos = new FileOutputStream("picture.pgm"); //create .pgm header //P2 fos.write('P'); fos.write('2'); //500x200 pixel fos.write(500); fos.write(200); //create black image for(int i=0; i< 500; i++){ for(int k=0; k< 200; k++){ fos.write(0); } } fos.close(); }catch(IOException e){ System.out.println(e); } } } 函数。该函数用于将 32 位整数转换为日期时间,它无法解析日期时间的字符串表示。

MySQL 确实存在 ISO8601 格式的问题。它可以读取它,但它在“Z”处变得混乱:

from_unixtime()

如果我们省略“Z”,一切都很好:

mysql> select '2021-05-06T02:21:20.000000Z' + interval 0 minute as d;
+---------------------+
| d                   |
+---------------------+
| 2021-05-06 02:21:20 |
+---------------------+
1 row in set,1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-------------------------------------------------------------------+
| Level   | Code | Message                                                           |
+---------+------+-------------------------------------------------------------------+
| Warning | 1292 | Truncated incorrect datetime value: '2021-05-06T02:21:20.000000Z' |
+---------+------+-------------------------------------------------------------------+

因此,只需在 SQL 中或在将 ISO8601 字符串值传递给 SQL 之前删除“Z”:

mysql> select '2021-05-06T02:21:20.000000' + interval 0 minute as d;
+---------------------+
| d                   |
+---------------------+
| 2021-05-06 02:21:20 |
+---------------------+
1 row in set (0.00 sec)