时间戳long 转String
时间: 2023-11-08 18:00:36 浏览: 146
将时间戳long转换为String的方法有多种。根据你提供的引用内容,可以采用以下方法之一:
1. 使用SimpleDateFormat类进行格式化:
```
// 假设时间戳为timestamp
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(new Date(timestamp));
```
2. 使用DateTimeFormatter类进行格式化(适用于Java 8及以上版本):
```
// 假设时间戳为timestamp
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateString = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).format(dtf);
```
请注意,使用哪种方法取决于你的具体需求以及所用的Java版本。
相关问题
long时间戳转string
要将long类型的时间戳转换为字符串,可以使用以下方法:
```
public static String convertTimeToString(Long time){
Assert.notNull(time, "time is null");
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
}
```
这个方法将接收一个Long类型的时间戳作为参数,并将其转换为指定格式的字符串。时间格式为"yyyy-MM-dd HH:mm:ss"。使用DateTimeFormatter将时间戳转换为LocalDateTime,然后使用format方法将其格式化为字符串。
例如,如果时间戳为1629504000000,使用convertTimeToString方法将其转换为字符串,结果为"2021-08-21 00:00:00"。
请注意,在使用此方法之前,需要确保时间戳不为null,并按照指定的格式为"yyyy-MM-dd HH:mm:ss"进行转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
Java将Long类型时间戳转成string
可以使用Java提供的SimpleDateFormat类将Long类型时间戳转换为String类型。
以下是示例代码:
```java
long timestamp = System.currentTimeMillis(); // 获取当前时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义时间格式
String dateStr = sdf.format(new Date(timestamp)); // 将时间戳转换为日期字符串
System.out.println(dateStr); // 输出日期字符串
```
其中,"yyyy-MM-dd HH:mm:ss"是时间格式,可以根据实际需要进行修改。
阅读全文
相关推荐













