java获取精确到秒的当前时间
时间: 2023-06-03 10:05:59 浏览: 151
当前时间可以使用Java中的Date类和System.currentTimeMillis()方法获得,可以通过以下方式获取当前精确到秒的时间:
```
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime()/1000;
System.out.println("当前时间:" + time);
}
}
```
相关问题
java 获取当前系统时间 精确到毫秒
在 Java 中,可以使用 `System.currentTimeMillis()` 方法获取当前系统时间的毫秒数,然后再将其转换为需要的时间格式。
以下是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class GetCurrentTime {
public static void main(String[] args) {
// 获取当前系统时间的毫秒数
long currentTimeMillis = System.currentTimeMillis();
// 将毫秒数转换为日期格式
Date date = new Date(currentTimeMillis);
// 定义日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
// 将日期格式化为字符串
String currentTime = sdf.format(date);
// 输出当前系统时间
System.out.println("当前系统时间:" + currentTime);
}
}
```
执行以上代码,可以输出当前系统时间,精确到毫秒。
Java 获取当前时间精确到毫秒
可以使用Java的System类和Date类来获取当前时间精确到毫秒。
示例代码如下:
```
import java.util.Date;
public class GetCurrentTimeMillisExample {
public static void main(String[] args) {
// 获取当前时间精确到毫秒
long currentTimeMillis = System.currentTimeMillis();
// 将时间戳转换为Date类型
Date currentDate = new Date(currentTimeMillis);
// 输出当前时间
System.out.println(currentDate);
}
}
```
在上面的示例中,我们首先使用System.currentTimeMillis()方法获取当前时间戳,然后将其转换为Date类型,最后输出当前时间。这样就可以获取当前时间精确到毫秒了。
相关推荐
![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)