判断时间段是否交叉(比如12:34~13:49与11:44~14:59)

代码不是很好,可以直接调用compareTo方法比较、大致思路就是先将其格式化然后再比较时间

/**
		 * 12:15~13:59 11:59~12:59 13:58-15:59与14:00-15:59
		 * 
		 * @param timeArr1
		 * @param timeArr2
		 * @return
		 */
		public boolean checkTimeJiaoCha(String timeArr1[], String timeArr2[]) {

			try {
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
				if (sdf.parse(timeArr2[0]).after(sdf.parse(timeArr1[0]))
						&& sdf.parse(timeArr2[0])
								.before(sdf.parse(timeArr1[1]))) {

					return false;

				} else if (sdf.parse(timeArr2[1]).after(sdf.parse(timeArr1[0]))
						&& sdf.parse(timeArr2[1])
								.before(sdf.parse(timeArr1[1]))) {

					return false;

					// 一样的时间
				} else if (sdf.parse(timeArr2[0]).compareTo(
						sdf.parse(timeArr1[0])) == 0
						&& sdf.parse(timeArr2[1]).compareTo(
								sdf.parse(timeArr1[1])) == 0) {

					return false;
				}
			} catch (ParseException e) {
				Log.e("----checkTimeJiaoCha in timezoneset---------", "时间格式化错误");
				e.printStackTrace();
			}
			return true;
		}

限制事件输入格式,这种并不是好的方式,但可以EditText监听输入,这个一般会经常用到,比如要求输入特定字符等。


/**
	 * 限制	Edittext的输入按照某种格式来
	 * 这里是:00:00 
	 * @author dalvikCoder
	 *
	 */
	class Watch implements TextWatcher {

		private CharSequence str;
		private int start;
		private int end;
		private String regArr[] = new String[] { "^[0-2]{1}$",
				"^[0-2]{1}[0-9]{1}$", "^[0-2]{1}[0-9]{1}:$",
				"^[0-2]{1}[0-9]{1}:[0-5]{1}$",
				"^[0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$" };// 19:59
		private Pattern pattern;
		private EditText et;
		private boolean flag = false;

		public Watch(EditText et) {

			this.et = et;
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			str = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

			start = et.getSelectionStart();
			end = et.getSelectionEnd();

			if (str.length() == 1) {

				pattern = Pattern.compile(regArr[0]);

				if (!pattern.matcher(str).matches()) {

					s.delete(start - 1, end);
					et.setText(s);
					et.setSelection(s.length());
					return;
				}
			} else if (str.length() == 2) {

				pattern = Pattern.compile(regArr[1]);

				if (!pattern.matcher(str).matches()) {

					s.delete(start - 1, end);
					et.setText(s);
					et.setSelection(s.length());
					flag = true;
					return;
				}
			} else if (str.length() == 3) {

				pattern = Pattern.compile(regArr[2]);

				if (!pattern.matcher(str).matches()) {

					// Common.Show(context, "该输入冒号了.");
					s.delete(start - 1, end);
					et.setText(s);
					et.setSelection(s.length());
					return;
				}
			} else if (str.length() == 4) {

				pattern = Pattern.compile(regArr[3]);

				if (!pattern.matcher(str).matches()) {

					s.delete(start - 1, end);
					et.setText(s);
					et.setSelection(s.length());
					return;
				}
			} else if (str.length() == 5) {

				pattern = Pattern.compile(regArr[4]);

				if (!pattern.matcher(str).matches()) {

					s.delete(start - 1, end);
					et.setText(s);
					et.setSelection(s.length());
					return;
				}
			}

		}
	}

格式为:00:00 正则表达式

"^[0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$"// 00:00 ~ 23:59

转载请注明出处。欢迎批评指正。

你可能感兴趣的:(Android开发)