获取当前时间,格式为yyyy-MM-dd HH:mm:ss,并转换为Date类型 java
时间: 2024-05-13 14:20:30 浏览: 90
js将日期格式转换为YYYY-MM-DD HH:MM:SS
可以使用以下代码获取当前时间,并将其转换为Date类型:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurrentTime {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date());
System.out.println("Current Time: " + currentTime);
try {
Date date = sdf.parse(currentTime);
System.out.println("Date object: " + date);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
输出:
```
Current Time: 2022-05-14 13:10:23
Date object: Sat May 14 13:10:23 CST 2022
```
阅读全文