java程序运行获取当前时间
时间: 2023-07-06 09:36:10 浏览: 65
获取当前时间可以使用Java中的Date类和DateFormat类。
以下是获取当前时间的示例代码:
```java
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class CurrentTimeExample {
public static void main(String[] args) {
// 创建Date对象,表示当前时间
Date now = new Date();
// 创建DateFormat对象,格式化时间
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化当前时间
String currentTime = df.format(now);
// 输出当前时间
System.out.println("当前时间:" + currentTime);
}
}
```
运行以上代码会输出当前时间,格式为“yyyy-MM-dd HH:mm:ss”。
相关问题
Java程序运行时间
Java程序的运行时间可以通过System.currentTimeMillis()方法来获取,该方法返回当前时间的毫秒数。可以在程序的开始和结束处分别调用该方法,然后计算两次调用的时间差来得到程序的运行时间。示例如下:
```
long startTime = System.currentTimeMillis();
// 程序代码
long endTime = System.currentTimeMillis();
long runTime = endTime - startTime;
System.out.println("程序运行时间:" + runTime + "毫秒");
```
java 打印程序运行时间
可以使用以下两种方法来打印Java程序的运行时间:
方法一:使用毫秒级别的计算
```java
long startTime = System.currentTimeMillis();
// 要计算的程序部分
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:" + (endTime - startTime) + "毫秒");
```
这种方法使用`System.currentTimeMillis()`来获取当前时间的毫秒数,然后计算程序运行时间的差值。
方法二:使用纳秒级别的计算
```java
long startTime_N = System.nanoTime();
// 要计算的程序部分
long endTime_N = System.nanoTime();
System.out.println("程序运行时间:" + (endTime_N - startTime_N) + "纳秒");
```
这种方法使用`System.nanoTime()`来获取当前时间的纳秒数,然后计算程序运行时间的差值。相比毫秒级别的计算,纳秒级别的计算更加精确。
根据你的需求选择适合的方法来打印程序运行时间即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [java记录代码运行时间毫秒微秒](https://blog.csdn.net/gotowest123/article/details/124567430)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Java计算一段程序的运行时间](https://blog.csdn.net/weixin_60414376/article/details/123857483)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![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_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)