java毫秒值转换成日期
时间: 2023-09-13 19:12:36 浏览: 313
要将Java的毫秒值转换为日期,可以使用Java中的Date类和SimpleDateFormat类。
以下是一个示例代码:
```java
long milliseconds = System.currentTimeMillis();
Date date = new Date(milliseconds);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
```
这将打印出当前日期和时间的字符串表示,格式为“yyyy-MM-dd HH:mm:ss”。可以根据需要更改日期格式。
相关问题
java毫秒值转换成LocalDate
要将Java的毫秒值转换为LocalDate,可以使用Java 8中的Instant类和LocalDate类。
以下是一个示例代码:
```java
long milliseconds = System.currentTimeMillis();
Instant instant = Instant.ofEpochMilli(milliseconds);
LocalDate date = instant.atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println(date);
```
这将打印出当前日期的字符串表示,格式为“YYYY-MM-DD”。可以根据需要更改日期格式。注意,这种转换只能得到日期,而没有时间信息。如果需要时间信息,可以使用LocalDateTime类。
java将毫秒值转为日期
可以使用`java.util.Date`和`java.text.SimpleDateFormat`来将毫秒值转换为日期。以下是一个示例代码:
```java
import java.util.Date;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
long milliseconds = 1622098800000L; // 毫秒值
// 创建一个Date对象
Date date = new Date(milliseconds);
// 创建一个SimpleDateFormat对象,设置日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 使用SimpleDateFormat对象格式化日期
String formattedDate = sdf.format(date);
// 输出格式化后的日期
System.out.println("转换后的日期为:" + formattedDate);
}
}
```
在上述示例中,我们将毫秒值`1622098800000L`转换为日期,并使用`SimpleDateFormat`将其格式化为`yyyy-MM-dd HH:mm:ss`的字符串形式。输出结果为:
```
转换后的日期为:2021-05-27 00:00:00
```
你可以根据需要调整日期格式。
阅读全文
相关推荐














