测试两题

package com.cqsw.untitled9.demo2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 代杭
 * @EMAIL 2366567504@qq.com
 * @QQ 2366567504
 * @PHONE 15123644617
 * @WECHAT dh5757124
 * @DESC
 */
public class Demo1 {
	public static void main(String[] args) {
		/*输入a数组*/
		int[] a = new int[]{1, 5, 8, 11, 16, 17, 29, 34, 39};
		/*目标time_line*/
		int[] time_line = new int[]{3, 7, 12, 17, 19, 31, 40};
		/*结果Map*/
		List<HashMap<String, Integer>> resultList = new ArrayList<>();
		/*排除数组a索引的集合*/
		List<Integer> excludeAArray = new ArrayList<>();
		for (int i : time_line) {
			/*找到当前这个元素的最近值*/
			int result = findTimeLineLately(i, a, excludeAArray);
			HashMap<String, Integer> itemMap = new HashMap<>();
			itemMap.put("line",i);
			itemMap.put("time",result);
			resultList.add(itemMap);
		}
		/*循环遍历,当前也可以直接在上面遍历,如果仅仅是遍历,那么干脆都可以不用resultList来存放结果*/
		for (HashMap<String, Integer> stringIntegerHashMap : resultList) {
			System.out.println(stringIntegerHashMap);
		}
	}

	/**
	 * @param target
	 * @param a
	 * @param excludeAArray
	 * @return
	 */
	public static int findTimeLineLately(int target, int[] a, List<Integer> excludeAArray) {
		/*找到符合标准的索引集合,如果有多个默认选最后一个*/
		List<Integer> indexList = new ArrayList<>();
		/*记录当前符合的值*/
		int conformValue = Integer.MAX_VALUE;
		for (int i = 0; i < a.length; i++) {
			boolean isHave = false;
			/*查看排除索引集合中是否存在,如果存在,则下一轮*/
			for (Integer integer : excludeAArray) {
				if (i == integer) {
					isHave = true;
				}
			}
			if (isHave) {
				continue;
			}
			/*拿到当前值*/
			int item = a[i];
			/*相差的值*/
			int value = Math.abs(target - item);
			/*如果当前符合的值比我们的相差值大,那么更新符合的值*/
			if (conformValue > value) {
				conformValue = value;
				/*找到了新的值,将符合标准的索引集合情况重新添加*/
				indexList.clear();
				indexList.add(i);
			} else if (conformValue == value) {
				/*如果相差值相同,那么再加入一个索引*/
				indexList.add(i);
			}
		}
		/*加入索引*/
		excludeAArray.add(indexList.get(indexList.size() - 1));
		/*返回最后一个数据*/
		return a[indexList.get(indexList.size() - 1)];
	}
}

package com.cqsw.untitled9.demo2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 代杭
 * @EMAIL 2366567504@qq.com
 * @QQ 2366567504
 * @PHONE 15123644617
 * @WECHAT dh5757124
 * @DESC
 * 大致思路:
 * 1、用户输入日期,转换为Date记作nowDate,其中可以做格式校验,try一下就行了。
 * 2、使用Calendar获取年、月、日、时、分、秒、星期,注意时要获取24小时制度的
 * 3、如果是星期天,则增加一天的时间,然后赋值给nowDate
 * 4、如果是星期六,则增加两天的时间,然后赋值给nowDate
 * 5、后面都是对小时数的判断,小时记作hour
 * 6、如果hour <= 8,则直接将hour看为8,然后加上三小时
 * 7、如果hour <= 12,那么hour+3,然后-12记作remainder,然后拿到当天下午的两点时间+remainder就是结果
 * 8、如果hour <= 14,则直接将hour看为14,然后加上三小时
 * 9、如果hour < 18,则hour+3-18记作remainder,然后拿到第二天的上午八点时间+remainder就是结果
 * 10、如果hour >= 18,则直接跳到第二天上午八点,然后加上三小时
 * 11、注意在第9和第10点的时候,可能出现第二天是星期六的情况,所以我们在printDate中再次判断了星期
 */
public class Demo2 {

	/*两种转换格式*/
	private static SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static void main(String[] args) throws ParseException {

		 Date nowDate = simpleDateFormat2.parse("2020-03-21 04:23:31");
		/*模拟当前输入时间,实际输入的时候转换为Date即可。*/
		// Date nowDate = new Date();
		/* 测试hour <= 8的情况,输出:2020-10-12 11:11:31 */
		// Date nowDate = simpleDateFormat2.parse("2020-10-16 7:11:31");
		/* 测试hour <= 12的情况,输出:2020-10-12 16:11:31*/
		// Date nowDate = simpleDateFormat2.parse("2020-10-10 11:11:31") ;
		/* 测试hour <= 14的情况,输出:2020-10-12 17:11:31*/
		// Date nowDate = simpleDateFormat2.parse("2020-10-10 13:11:31") ;
		/* 测试hour < 18并且第二天是星期六的情况,输出:2020-10-13 08:11:31*/
		// Date nowDate = simpleDateFormat2.parse("2020-10-16 15:11:31") ;
		/* 测试hour >= 18并且第二天是星期六的情况,输出:2020-10-13 11:11:31*/
		// Date nowDate = simpleDateFormat2.parse("2020-10-16 19:11:31") ;

		System.out.println("输入日期:" + simpleDateFormat2.format(nowDate));
		/*判断是否为周一~周五,如果是,那么加上两天时间*/
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(nowDate);
		/*获得星期,1~7:对应日~六*/
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		/*如果星期日,加入1天*/
		if (week == 1) {
			nowDate = new Date(nowDate.getTime() + 1000 * 60 * 60 * 24);
		} else if (week == 7) {
			/*星期六,加入两天*/
			nowDate = new Date(nowDate.getTime() + 1000 * 60 * 60 * 24 * 2);
		}
		/*更换天数后重新设置值*/
		calendar.setTime(nowDate);
		/*========此时一定是周一~周五========*/
		/*获得小时*/
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		/*分秒毫秒*/
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		int mm = calendar.get(Calendar.MILLISECOND);
		/*
		 * 如果hour <= 8,那么我们直接给它跳到八点,因为休息时间忽略掉,然后加上三小时,也就是十一点。
		 * 样例输入:(例如不考虑周末周六,只考虑时分秒的情况)
		 *  04:11:31,先转换为08:11:31,然后+3=>11:11:31
		 * */
		if (hour <= 8) {
			nowDate = new Date(toYearMonthDayDate(nowDate).getTime() + 1000 * 60 * 60 * 11 + minute * 60 * 1000 + second * 1000 + mm);
			printDate(nowDate,calendar);
			return;
		}
		/*
		 * 如果小于12,那么说明就是9~12之间,+3以后,直接转到两点,然后加完后的值-12,然后用下午两点加上余数,并加上分秒毫秒。
		 * 样例输入:(例如不考虑周末周六,只考虑时分秒的情况)
		 *      10:11:11,+3后得13:11:11,然后13-12得1.
		 *      然后14:00:00+1小时11分11秒=>15:11:11
		 * */
		if (hour <= 12) {
			int remainder = hour + 3 - 12;
			nowDate = new Date(toYearMonthDayDate(nowDate).getTime() + 1000 * 60 * 60 * (14 + remainder) + minute * 60 * 1000 + second * 1000 + mm);
			printDate(nowDate,calendar);
			return;
		}
		/*如果hour <= 14,那么全都按照14来算,然后加入3小时*/
		if (hour <= 14) {
			nowDate = new Date(toYearMonthDayDate(nowDate).getTime() + 1000 * 60 * 60 * 17 + minute * 60 * 1000 + second * 1000 + mm);
			printDate(nowDate,calendar);
			return;
		}
		/**
		 * 如果 hour < 18,那么直接相加,然后-18,多出来的时间放到第二天去加上就可以了,
		 * 例如:(例如不考虑周末周六,只考虑时分秒的情况)
		 *      2020-11-11 17:11:11 ->+3后 20:11:11,然后20-18等于2,然后到第二天2020-11-12 8:11:11+2 => 最终结果就位2020-11-12 10:11:11
		 */
		if (hour < 18) {
			int remainder = hour + 3 - 18;
			nowDate = new Date(toYearMonthDayDate(nowDate).getTime() + 1000 * 60 * 60 * (32 + remainder) + minute * 60 * 1000 + second * 1000 + mm);
			/*防止到星期六的情况,所以重新设置Time*/
			calendar.setTime(nowDate);
			printDate(nowDate,calendar);
			return;
		}
		/*
		 * 如果hour >= 18,那么直接转到第二天上午八点,然后加上三小时,
		 * 例如:(例如不考虑周末周六,只考虑时分秒的情况)
		 *      9-11 19:11:11,直接转到第二天,9-12 8:11:11,然后加3=9-12 11:11:11*/
		if (hour >= 18) {
			nowDate = new Date(toYearMonthDayDate(nowDate).getTime() + 1000 * 60 * 60 * 35 + minute * 60 * 1000 + second * 1000 + mm);
			/*防止到星期六的情况,所以重新设置Time*/
			calendar.setTime(nowDate);
			printDate(nowDate,calendar);
			return;
		}
		printDate(nowDate,calendar);
	}

	/**
	 * 将"yyyy-MM-dd HH:mm:ss格式的date转换为"yyyy-MM-dd的date,时分秒直接去掉不要
	 *
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date toYearMonthDayDate(Date date) throws ParseException {
		return simpleDateFormat1.parse(simpleDateFormat2.format(date).split(" ")[0]);
	}

	/**
	 * 打印Date
	 *
	 * @param date
	 */
	private static void printDate(Date date, Calendar calendar) {
		/*星期六的情况*/
		if (calendar.get(Calendar.DAY_OF_WEEK) == 7) {
			/*星期六,加入两天*/
			date = new Date(date.getTime() + 1000 * 60 * 60 * 24 * 2);
		}
		System.out.println("结果:" + simpleDateFormat2.format(date));
	}
}


原文地址:https://www.cnblogs.com/daihang2366/p/13806003.html