Java计算两个日期的时间间隔
不多说,直接上代码
1、利用SimpleDateFormat类,获取天数间隔
代码:
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 利用SimpleDateFormat类计算两个时间的天数间隔 * @throws ParseException */ public class CalculateDaysInterval1 { public static void main(String[] args) throws ParseException { // 日期格式化 DateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd"); Date startDate = simpleFormat.parse("2021-03-01"); Date endDate = simpleFormat.parse("2021-07-08"); long startTime = startDate.getTime(); long endTime = endDate.getTime(); int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24)); System.out.println("两个时间之间的天数间隔为:" + days); } }
输出结果:
两个时间之间的天数间隔为:129
2、利用Java 8中ChronoUnit类,获取天数间隔
代码:
import java.time.LocalDate; import java.time.temporal.ChronoUnit; /** * 利用ChronoUnit类计算两个时间的天数间隔 */ public class CalculateDaysInterval2 { public static void main(String[] args) { LocalDate startDate = LocalDate.of(2021, 3, 1); LocalDate endDate = LocalDate.of(2021, 7, 8); long days = ChronoUnit.DAYS.between(startDate, endDate); System.out.println("两个时间之间的天数间隔为:" + days); } }
输出结果:
两个时间之间的天数间隔为:129
3.1、利用getTime(),获取时分秒间隔
public static void main(String[] args) throws ParseException { String str1 = "2021-09-09 03:30:16"; String str2 = "2021-09-09 13:31:19"; DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date firstTime = df.parse(str1); Date currentTime = df.parse(str2); System.out.println(getTimeInterval(currentTime, firstTime)); } /** * 获取时间差方法,返回时分秒 HH:mm:ss * * @param currentTime * @param firstTime * @return */ public static String getTimeInterval(Date currentTime, Date firstTime) { DecimalFormat decimalFormat = new DecimalFormat("00"); long diff = currentTime.getTime() - firstTime.getTime();//得到的差值 long hours = diff / (1000 * 60 * 60); //获取时 long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60); //获取分钟 long s = (diff / 1000 - hours * 60 * 60 - minutes * 60);//获取秒 String countTime = "" + decimalFormat.format(hours) + ":" + decimalFormat.format(minutes) + ":" + decimalFormat.format(s); return countTime; }
输出结果:
3.2、利用getTime(),获取年月日时分秒间隔
public static void main(String[] args) throws ParseException { String str1 = "2020-05-29 03:30:16"; String str2 = "2021-09-09 13:31:19"; DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date firstTime = df.parse(str1); Date currentTime = df.parse(str2); System.out.println(getTimeInterval(currentTime, firstTime)); } /** * 获取时间差方法,返回年月日时分秒 某年某月某日 某时某分某秒 * * @param currentTime * @param firstTime * @return */ public static String getTimeInterval(Date currentTime, Date firstTime) { // 得到的时间差值, 微秒级别 long diff = currentTime.getTime() - firstTime.getTime(); Calendar currentTimes = dataToCalendar(currentTime);//当前系统时间转Calendar类型 Calendar firstTimes = dataToCalendar(firstTime);//查询的数据时间转Calendar类型 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; } // Date类型转Calendar类型 public static Calendar dataToCalendar(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; }
输出结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端