SimpleDateFormat求时间差
时间: 2023-11-22 11:12:39 浏览: 31
SimpleDateFormat类本身并不能直接求解时间差。它是一个用于日期格式化和解析的类,用于将日期对象转换为指定格式的字符串,或者将字符串解析为日期对象。要计算时间差,你可以使用Date类的getTime()方法将日期对象转换为毫秒数,然后进行计算。下面是一个示例代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date startDate = sdf.parse("2003/05/09 00:00");
Date endDate = sdf.parse("2015/01/26 00:00");
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long timeDifference = endTime - startTime;
请注意,这个方法计算的是毫秒级别的差值,如果需要以天、小时、分钟等单位表示时间差,你可以将毫秒数转换为相应的单位。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Java计算时间差、日期差总结](https://blog.csdn.net/sy793314598/article/details/79544796)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Android利用SimpleDateFormat计算时间差获得消耗时长](https://blog.csdn.net/ymtianyu/article/details/106661826)[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^v93^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)
![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)
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)