两个时间计算【十分秒】
- 用于计算接口的执行时长
| package com.chinalife.maced_bdsdk; |
| |
| import java.text.DateFormat; |
| import java.text.ParseException; |
| import java.text.SimpleDateFormat; |
| import java.util.Calendar; |
| import java.util.Date; |
| |
| public class HaoShiDemo { |
| public static void main(String[] args) throws ParseException, InterruptedException { |
| |
| Date start = new Date(); |
| Thread.sleep(1000); |
| Date end = new Date(); |
| |
| DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| |
| String start1 = df.format(start); |
| String end2 = df.format(end); |
| |
| System.out.println("HaoShi--->:" + "Group:" + MainDate(end2, start1)); |
| } |
| |
| public static String MainDate(String StartDate, String EndDate) throws ParseException { |
| DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| |
| Date currentTime = df.parse(StartDate); |
| Date firstTime = df.parse(EndDate); |
| String str = getTime(currentTime, firstTime); |
| |
| return str; |
| } |
| |
| |
| public static String getTime(Date currentTime, Date firstTime) { |
| long diff = currentTime.getTime() - firstTime.getTime(); |
| Calendar currentTimes = dataToCalendar(currentTime); |
| Calendar firstTimes = dataToCalendar(firstTime); |
| int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR); |
| int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH); |
| int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH); |
| if (day < 0) { |
| month -= 1; |
| currentTimes.add(Calendar.MONTH, -1); |
| day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH); |
| } |
| if (month < 0) { |
| month = (month + 12) % 12; |
| year--; |
| } |
| long days = diff / (1000 * 60 * 60 * 24); |
| long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); |
| long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60); |
| long s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60); |
| String CountTime = "" + year + "年" + month + "月" + day + "天" + hours + "小时" + minutes + "分" + s + "秒"; |
| return CountTime; |
| } |
| |
| |
| public static Calendar dataToCalendar(Date date) { |
| Calendar calendar = Calendar.getInstance(); |
| calendar.setTime(date); |
| return calendar; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~