首页 > 代码库 > 人性化合理的时间选择逻辑

人性化合理的时间选择逻辑

最近做一个项目,有一个时间选择控件,里面的时间需要合理化设置。


比如,最多支持 今天,明天。

上班的合理时间为 0 - 11点

下班的合理时间为12 - 23 点


根据系统所处时间 AM 或者 PM ,判断 上班的话只好约到明天,因为处于PM没有办法再约上班的了。

另外顾客下单的时间应该比系统时间多出半个小时的缓冲时间。分钟只能选 00,10,20,30,40,50  这些段。


估计大家看到还是不明白,因为的确业务逻辑有点复杂的。可以参考 人人拼车 ,天天用车应用约车时的时间选择控件

逻辑代码如下,其实主要是自己做个记录:

/**
 * 计算
 * 假设 1. 系统时间精确
 *    2. 最小预约时间为标准时间+缓冲时间
 * @author lq
 *
 */
public class Duty {
	
	public static int INT_DUTY = 1 ;
	public static int BUFFER_TIME = 30 ;
	public static String STR_AM = "AM";
	public static String STR_PM = "PM";
	public static String STR_TODAY = "今天";
	public static String STR_TOMORROW = "明天";
	public static int INT_DUTTY_LIMIT_NOON = 12;
	public static int INT_DUTTY_LIMIT_DAWN = 24;
	
		public static void main(String[] args) {
			
		//测试
			
			String[] legalDay = dutyLegalDay(INT_DUTY);
			if(INT_DUTY==0){
				System.out.println("预约上班");
			}else{
				System.out.println("预约下班");
			}
			for (int i = 0; i < legalDay.length; i++) {
				System.out.println("legal day :   "+legalDay[i]);
				String[] dutyLegalHour = dutyLegalHour(INT_DUTY,legalDay[i]);
				for (int j = 0; j < dutyLegalHour.length; j++) {
					System.out.println("legal Hour :   "+dutyLegalHour[j]);
							String[] dutyLegalMinute = dutyLegalMinute(INT_DUTY, legalDay[i], dutyLegalHour[j]);
							for (int k = 0; k < dutyLegalMinute.length; k++) {
								System.out.println("legal Minute :   "+dutyLegalMinute[k]);
							}
				}
			}
			
		}
		
		
		/**
		 * 计算天
		 * @param interval
		 * @param curTime
		 * @param minTime
		 * @param duty   0 代表上班  1代表下班
		 */
		public static String[] dutyLegalDay(int duty){
			LinkedList<String> listDay = new LinkedList<String>();
			String[] arrayLegalDay = null ;
			int[] array = getMinHourMinute();
			
			if(duty==0){ //上班
				if(STR_AM.endsWith(JudgeAMPM()) && array[0] < INT_DUTTY_LIMIT_NOON) //  预约上班合理性 1. 现在时间是上午  2. 合理最小时间 应该为小时小于12 ,若是PM 则仅可以预约明天的
				{
					listDay.add(STR_TODAY+getWeek(getTodayDate()));
				}
			}else{//下班
				if(array[0] < INT_DUTTY_LIMIT_DAWN) //   合理最小时间 应该为小时小于23,时间区间是 12 - 23 ,若是PM 则仅可以预约明天的
				{
					listDay.add(STR_TODAY+getWeek(getTodayDate()));
				}
			}
			listDay.add(STR_TOMORROW+getWeek(getTomorrowDate()));
			arrayLegalDay = new String[listDay.size()] ;
			listDay.toArray(arrayLegalDay);
			
//			for (int i = 0; i < arrayLegalDay.length; i++) {
//				System.out.println("legal Day :   "+listDay.get(i));
//			}
			
			return arrayLegalDay ;
		}
		
		/**
		 * 计算上班合法小时
		 * 
		 */
		public static String[] dutyLegalHour(int duty,String day){
			LinkedList<String> listHour = new LinkedList<String>();
			
			String[] arrayLegalHour = null ;
			int[] array = getMinHourMinute();
			
			if(duty==0){ //上班
				//传递过来  今天 or 明天
				if(day.contains(STR_TODAY) && array[0] < INT_DUTTY_LIMIT_NOON){//时间区间是 0 -11
					//合法的
				}else{
					//明天的,或者超过12的,都归为明天,
					array[0] = 0 ;
				}
				
				for (int i = array[0]; i <= 11; i++) {
					listHour.add(i+"");
				}
			}else{ //下班
				//传递过来  今天 or 明天
				if(day.contains(STR_TODAY) && array[0] < INT_DUTTY_LIMIT_DAWN){//时间区间是 12 -23
					if(array[0] < INT_DUTTY_LIMIT_NOON){ 
						array[0] = INT_DUTTY_LIMIT_NOON ;
					}
				}else{
					//明天的,或者超过23的,都归为明天,
					array[0] = INT_DUTTY_LIMIT_NOON ;
				}
				
				for (int i = array[0]; i <= 23; i++) {
					listHour.add(i+"");
				}
			}
			
			arrayLegalHour = new String[listHour.size()] ;
			listHour.toArray(arrayLegalHour);
//			for (int i = 0; i < listHour.size(); i++) {
//				System.out.println("legal Hour :   "+listHour.get(i));
//			}
			
			return arrayLegalHour ;
			
		}
		
		/**
		 * 计算合法的分
		 * @param minute
		 * @return
		 */
	    public static String[] dutyLegalMinute(int duty ,String day,String hour){
	    	LinkedList<String> listMinute= new LinkedList<String>();
	    	String[] arrayLegalMinute =null;
	    	int[] array = getMinHourMinute();
			if(duty==0){
				if(day.contains(STR_TODAY)){
					if(Integer.parseInt(hour) > array[0]){
						array[1] = 0 ;
					}
				}else{
					array[1] = 0 ;
				}
				
				for (int i = array[1]; i <= 5; i++) {
					listMinute.add(i+"0");
				}
			}else{
				
				if(day.contains(STR_TODAY)){
					if(Integer.parseInt(hour) > array[0]){
						array[1] = 0 ;
					}
				}else{
						array[1] = 0 ;
				}
				
				for (int i = array[1]; i <= 5; i++) {
					listMinute.add(i+"0");
				}
			}
			arrayLegalMinute = new String[listMinute.size()] ;
			listMinute.toArray(arrayLegalMinute);
	    	
//			for (int i = 0; i < listMinute.size(); i++) {
//				System.out.println("legal Minute :   "+listMinute.get(i));
//			}
			
	    	return arrayLegalMinute;
	    }
		
		
	    
		
		/**
		 * 通用方法,得到最小小时 和分钟
		 * 计算合理的天、时、分 都需要用
		 * 返回结果  array[0] 最小时
		 *       array[1] 最小十分位
		 */
		public static int[] getMinHourMinute(){
			int[] array = new int[2] ;
			Date minTime =  getLegalTime(BUFFER_TIME);
			//最大值为  yyyy-MM-dd 11:20   因为 11:20 + buffer_time  = 11: 50
			int minHour = minTime.getHours();
			int minMinute = minTime.getMinutes();
			String strMinute = "";
			if(minMinute<10){
				strMinute = "0"+minMinute;
			}else{
				strMinute = ""+minMinute;
			}
			int[] arraytemp = splitDoubleDigit(strMinute);
			
			//array[0] 十位   array[1]个位
			if(arraytemp[1]>0){////只要个位大于0 ,都要十位进位,例如 52 要进成 60 , 31进位成 40
				arraytemp[0] =  arraytemp[0] + 1;
				 if(arraytemp[0] == 6){//是6,则小时+1 , 分钟的个位,十位归为0
					 minHour = minHour + 1 ;
					 arraytemp[0] = 0 ;
					 arraytemp[1] = 0 ;
				 }
			}
			array[0] = minHour ;
			array[1] = arraytemp[0];
			
			return array ;
		}
		
		
		/**
		 * 获得合法时间
		 * @param minute
		 * @return
		 */
		public static Date getLegalTime(int minute){
			 Date date = new Date();
			 Calendar c = Calendar.getInstance();
			 c.setTime(date);
			 c.add(Calendar.MINUTE, minute);
			 Date time = c.getTime();
			 return time;
		 }
		
		
		/**
		 * 判断当前是上午还是下午
		 * 
		 * @return
		 */
		public static String JudgeAMPM(){
			String ret = null ;
			
			GregorianCalendar calendar = new GregorianCalendar();  
			int val = calendar.get(GregorianCalendar.AM_PM) ;
			if(0==val){
				ret = STR_AM ;
			}else{
				ret = STR_PM ;
			}
			return ret ;
		}
		
		 /**
		  * 拆分两位数 例如  54  拆分为 5 , 4
		  * @param num
		  * @return
		  */
		 public static int[] splitDoubleDigit(String num){
				int[] resArray = new int[(num).length()];
				int number = Integer.parseInt(num);
				for(int i = resArray.length-1;i>=0;i--){
					resArray[i] = number%10;
					number /= 10;
				}
				/*for(int i:resArray){
					System.out.println(i);
				}*/
				return resArray; 
		 }
		 
		 
			//根据日期取得星期几  
			public static String getWeek(String date){   
				    Date temp = getDateByStr2(date);
			        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");  
			        String week = sdf.format(temp);  
			        return week;  
			    } 
			
			
			/**
			 * yyyy-MM-dd 转换成date数据
			 * @param dd
			 * @return
			 */
			public static Date getDateByStr2(String dd)  
			 {  
			  
			  SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");  
			  Date date;  
			  try {  
			   date = sd.parse(dd);  
			  } catch (ParseException e) {  
			   date = null;  
			   e.printStackTrace();  
			  }  
			  return date;  
			 }
			
			 /**
			  * 获取今天日期
			  * @return
			  */
			 public static String getTodayDate(){
				 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
				 return df.format(new Date());
			 }
			 
			 /**
			  * 获取明天日期
			  * 可以扩展
			  * @return
			  */
			 public static String getTomorrowDate(){
				 Date date=new Date();//取时间
				 Calendar calendar = new GregorianCalendar();
				 calendar.setTime(date);
				 calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
				 date=calendar.getTime();
				 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				 String dateString = formatter.format(date);
				 return dateString;
			 }
			 
		
}

现在系统时间为 2015-1-23 15:12 ,分别选择上班和下班。看展示结果。

上班:

预约上班
legal day :   明天星期六
legal Hour :   0
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   1
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   2
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   3
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   4
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   5
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   6
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   7
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   8
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   9
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   10
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   11
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50

预约下班:

预约下班
legal day :   今天星期五
legal Hour :   15
legal Minute :   50
legal Hour :   16
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   17
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   18
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   19
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   20
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   21
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   22
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   23
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal day :   明天星期六
legal Hour :   12
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   13
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   14
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   15
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   16
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   17
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   18
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   19
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   20
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   21
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   22
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50
legal Hour :   23
legal Minute :   00
legal Minute :   10
legal Minute :   20
legal Minute :   30
legal Minute :   40
legal Minute :   50



人性化合理的时间选择逻辑