java输出上次运行的开始时间和结束时间
时间: 2023-09-07 21:14:30 浏览: 84
可以使用java中的System类和Date类来实现输出上次运行的开始时间和结束时间的功能。
首先,在程序启动时记录开始时间,可以使用如下代码:
```
Date startTime = new Date();
```
然后,在程序结束时记录结束时间,可以使用如下代码:
```
Date endTime = new Date();
```
最后,将开始时间和结束时间格式化为字符串并输出即可,完整代码如下:
```
import java.util.Date;
public class LastRunTime {
public static void main(String[] args) {
// 记录开始时间
Date startTime = new Date();
// 程序代码
// ...
// 记录结束时间
Date endTime = new Date();
// 格式化时间并输出
System.out.println("上次运行开始时间:" + startTime.toLocaleString());
System.out.println("上次运行结束时间:" + endTime.toLocaleString());
}
}
```
在程序下次启动时,可以读取上次运行结束时间,与当前时间相减即可得到运行时长。
相关问题
用java中的properties输出上次运行代码的开始和结束时间
可以使用Java中的Properties类来保存上次运行代码的开始和结束时间,然后通过读取Properties文件获取这些信息。下面是一个示例代码:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class LastRunTime {
private static final String PROP_FILE = "lastruntime.properties";
private static final String START_TIME_PROP = "starttime";
private static final String END_TIME_PROP = "endtime";
public static void main(String[] args) {
Properties props = new Properties();
// 读取上次运行的开始时间和结束时间
try (FileInputStream fis = new FileInputStream(PROP_FILE)) {
props.load(fis);
} catch (IOException e) {
// 如果文件不存在,则创建一个新的Properties对象
System.out.println("No last run time found.");
}
// 获取本次运行的开始时间
Date startTime = new Date();
String startTimeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime);
System.out.println("Start time: " + startTimeStr);
// 如果上次运行的结束时间存在,则输出上次运行的结束时间
if (props.containsKey(END_TIME_PROP)) {
String lastEndTimeStr = props.getProperty(END_TIME_PROP);
System.out.println("Last end time: " + lastEndTimeStr);
}
// 模拟代码运行
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 获取本次运行的结束时间
Date endTime = new Date();
String endTimeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime);
System.out.println("End time: " + endTimeStr);
// 保存本次运行的开始时间和结束时间
props.setProperty(START_TIME_PROP, startTimeStr);
props.setProperty(END_TIME_PROP, endTimeStr);
try (FileOutputStream fos = new FileOutputStream(PROP_FILE)) {
props.store(fos, "Last run time");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,我们使用了一个名为“lastruntime.properties”的Properties文件来保存上次运行的开始时间和结束时间。在程序启动时,我们首先尝试读取这个文件中的内容。如果文件不存在,则说明这是第一次运行程序,我们会创建一个新的Properties对象。然后,我们获取本次运行的开始时间,并输出上次运行的结束时间(如果存在)。接着,我们模拟代码的运行。最后,我们获取本次运行的结束时间,并保存本次运行的开始时间和结束时间到Properties文件中。
用java输出上次运行程序所用时间
你可以使用System.currentTimeMillis()方法来获取当前时间的毫秒数,然后在程序运行结束时再获取一次时间的毫秒数,两者相减得到程序运行时间。具体实现代码如下:
```java
public class Main {
public static void main(String[] args) {
long startTime = System.currentTimeMillis(); // 获取程序开始时间
// 执行程序代码
long endTime = System.currentTimeMillis(); // 获取程序结束时间
long totalTime = endTime - startTime; // 计算程序运行时间
System.out.println("程序运行时间为:" + totalTime + "毫秒");
}
}
```
需要注意的是,System.currentTimeMillis()方法返回的是1970年1月1日00:00:00到当前时间的毫秒数,因此计算程序运行时间的结果也是毫秒数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)