欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java中累计时间的计算(以小时为最终结果)

程序员文章站 2024-03-07 13:09:03
...

最近在项目中需要用到工作累计时间,所以就就写了一个小方法,作用是可以计算两个时间点之间的累积时间(即累积工作小时),这里头需要注意的是输入的日期格式为

“yyyy-MM-dd-HH”,目前测试过后,可以计算不同年月日之间的小时差,下面附上主要代码:

public static int workHours(String startTime,String endTime){
		String[] startTimes = getStrings(startTime);
		for (int i = 0; i < startTimes.length; i++) {
			System.out.print(startTimes[i] + " ");
		}
		System.out.println();
		System.out.println("------------------------------");
		String[] endTimes = getStrings(endTime);
		for (int i = 0; i < endTimes.length; i++) {
			System.out.print(endTimes[i] + " ");
		}
		int workHours = 0;
		int workDays = 0;
		if (startTimes[0].equals(endTimes[0])) {// 年
			if (startTimes[1].equals(endTimes[1])) {// 月
				if (startTimes[2].equals(endTimes[2])) {// 同一天的不同小时
					workHours = Integer.parseInt(endTimes[3]) - Integer.parseInt(startTimes[3]);
				} else {// 同一月不同的一天
					workDays = Integer.parseInt(endTimes[2]) - Integer.parseInt(startTimes[2]);
					workHours = workDays * 24 + Integer.parseInt(endTimes[3]) - Integer.parseInt(startTimes[3]);
				}
			} else {// 同一年不同月
				String startMs = startTime.substring(0, 10);
				String endMs = endTime.substring(0, 10);
				Date earlydate = new Date();
				Date latedate = new Date();
				DateFormat df = DateFormat.getDateInstance();
				try {
					earlydate = df.parse(startMs);
					latedate = df.parse(endMs);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				workDays = daysBetween(earlydate, latedate);
				workHours = workDays * 24 + Integer.parseInt(endTimes[3]) - Integer.parseInt(startTimes[3]);
			}
		}else {//不同的年
			String startMs = startTime.substring(0, 10);
			String endMs = endTime.substring(0, 10);
			Date earlydate = new Date();
			Date latedate = new Date();
			DateFormat df = DateFormat.getDateInstance();
			try {
				earlydate = df.parse(startMs);
				latedate = df.parse(endMs);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			workDays = differentDays(earlydate, latedate);
			workHours = workDays * 24 + Integer.parseInt(endTimes[3]) - Integer.parseInt(startTimes[3]);
		}
		System.out.println();
		System.out.println("------------------------------");
		System.out.println(workDays);
		System.out.println(workHours);
		return workHours;
	}
其中用到的两个方法如下:

public static String[] getStrings(String time) {
		String[] s = time.split("-");
		return s;
	}
	public static final int daysBetween(Date early, Date late) {
		java.util.Calendar calst = java.util.Calendar.getInstance();
		java.util.Calendar caled = java.util.Calendar.getInstance();
		calst.setTime(early);
		caled.setTime(late);
		// 设置时间为0时
		calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
		calst.set(java.util.Calendar.MINUTE, 0);
		calst.set(java.util.Calendar.SECOND, 0);
		caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
		caled.set(java.util.Calendar.MINUTE, 0);
		caled.set(java.util.Calendar.SECOND, 0);
		// 得到两个日期相差的天数
		int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
		return days;
	}
	public static int differentDays(Date date1, Date date2) {
		Calendar calst = Calendar.getInstance();
		calst.setTime(date1);
		Calendar caled = Calendar.getInstance();
		caled.setTime(date2);
		int day1 = calst.get(Calendar.DAY_OF_YEAR);
		int day2 = caled.get(Calendar.DAY_OF_YEAR);
		int year1 = calst.get(Calendar.YEAR);
		int year2 = caled.get(Calendar.YEAR);
		calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
		calst.set(java.util.Calendar.MINUTE, 0);
		calst.set(java.util.Calendar.SECOND, 0);
		caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
		caled.set(java.util.Calendar.MINUTE, 0);
		caled.set(java.util.Calendar.SECOND, 0);
		if (year1 != year2) // 同一年
		{
			int timeDistance = 0;
			for (int i = year1; i < year2; i++) {
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {// 闰年
					timeDistance += 366;
				} else {// 不是闰年
					timeDistance += 365;
				}
			}
			return timeDistance + (day2 - day1);
		} else {// 不同年
			System.out.println("判断day2 - day1 : " + (day2 - day1));
			return day2 - day1;
		}
	}



相关标签: java 累计时间