日期格式转为string
时间: 2023-11-18 19:04:39 浏览: 37
可以使用datetime模块中的strftime方法将日期格式转为string。例如:
```python
import datetime
date = datetime.datetime.now()
date_str = date.strftime("%Y-%m-%d %H:%M:%S")
print(date_str)
```
输出结果为:2021-08-17 14:30:00
其中,strftime方法中的参数"%Y-%m-%d %H:%M:%S"表示将日期格式化为年-月-日 时:分:秒的形式。
相关问题
SQL将时间格式转换成String类型去掉特殊符号只留下数字
在 SQL 中,可以使用 CAST 函数将时间类型转换为字符串类型。然后可以使用 REPLACE 函数去掉特殊符号,只留下数字。以下是一个例子:
假设有一个名为 `my_table` 的表,其中有一个名为 `my_time` 的时间类型列。要将其转换为字符串类型并去掉特殊符号,可以使用以下 SQL 语句:
```sql
SELECT REPLACE(REPLACE(REPLACE(CAST(my_time AS VARCHAR), '-', ''), ' ', ''), ':', '') AS my_time_str
FROM my_table;
```
这里使用了三个 REPLACE 函数,分别将破折号、空格和冒号替换为空字符串,从而去掉了特殊符号。最终得到的字符串只包含数字。
String类型的时间格式转换成long类型
可以使用Java中的SimpleDateFormat类将String类型的时间格式转换成Date类型,然后再使用Date类的getTime()方法转换成long类型,示例代码如下:
```java
String time = "2021-10-01 12:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(time);
long timeInMillis = date.getTime();
System.out.println(timeInMillis);
} catch (ParseException e) {
e.printStackTrace();
}
```
在上面的代码中,我们首先定义了一个时间字符串,然后创建了一个SimpleDateFormat对象来指定时间的格式。接着,我们使用parse()方法将时间字符串解析成Date类型,最后通过getTime()方法获取时间的毫秒数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)