java将时间戳的字符串转为时间
时间: 2023-08-30 10:06:16 浏览: 65
可以使用Java中的SimpleDateFormat类将时间戳的字符串转换为时间。示例如下:
```java
String timestampStr = "1620100568";
long timestamp = Long.parseLong(timestampStr);
Date date = new Date(timestamp * 1000L); // 注意时间戳单位为秒,需要乘以1000转为毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
```
输出结果为:2021-05-04 14:09:28
其中,SimpleDateFormat中的日期格式可以根据需要进行修改。
相关问题
scala将字符串时间转为时间戳
在 Scala 中,可以使用 `java.text.SimpleDateFormat` 类来将字符串时间转换为时间戳。以下是一个简单的示例代码:
```scala
import java.text.SimpleDateFormat
import java.util.Date
val dateString = "2021-10-01 12:30:00"
val dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val date = dateFormat.parse(dateString)
val timestamp = date.getTime()
```
在这个示例中,我们首先定义了一个字符串时间 `dateString`,然后创建了一个 `SimpleDateFormat` 对象 `dateFormat`,用于指定时间格式。接着,我们使用 `dateFormat.parse` 方法将字符串时间转换为 `Date` 对象 `date`,最后使用 `date.getTime` 方法获取时间戳 `timestamp`。
需要注意的是,上述代码中的时间格式 `"yyyy-MM-dd HH:mm:ss"` 是一种常见的时间格式,如果你要转换的时间格式不同,需要修改相应的时间格式字符串。
java 时间戳转字符串
要将Java的时间戳转换为字符串,你可以使用Java的SimpleDateFormat类和Date类来实现。首先,你需要创建一个SimpleDateFormat的实例,指定你希望的日期格式。然后,你可以使用Date类的getTime()方法将时间戳转换为Date对象,最后使用SimpleDateFormat类的format()方法将Date对象格式化为字符串。
以下是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimestampToString {
public static void main(String[] args) {
long timestamp = 1291778220L;
Date date = new Date(timestamp * 1000); // 时间戳单位是秒,所以要乘以1000转为毫秒
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 指定日期格式
String dateString = sdf.format(date); // 将Date对象格式化为字符串
System.out.println(dateString);
}
}
```
上述代码将时间戳`1291778220`转换为字符串`2010-12-08 11:17:00`。你可以根据自己的需要更改日期格式,如`yyyy年MM月dd日 HH时mm分ss秒`等。<span class="em">1</span><span class="em">2</span>
#### 引用[.reference_title]
- *1* *2* [时间戳与字符串相互转换(JAVA)](https://blog.csdn.net/dgq227/article/details/13091987)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)