判断输入的时间格式和判断当前输入的时间是否在输入的时间范围内
2017-09-08 10:43
429 查看
package com.itcast.demo; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Scanner; public class DateTest { /** * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String st1=sc.nextLine(); String st2=sc.nextLine(); if(isInTime(st1,st2)){ System.out.println("当前时间在时间范围内"); }else{ System.out.println("当前时间不在时间范围内"); } } public static boolean isValidDate(String str) { boolean convertSuccess=true; SimpleDateFormat format = new SimpleDateFormat("HH:mm"); try { format.setLenient(false); format.parse(str); } catch (ParseException e) { convertSuccess=false; } return convertSuccess; } /** * 判断某一时间是否在一个区间内 * * @param sourceTime * 时间区间,半闭合,如[10:00-20:00) * @param curTime * 需要判断的时间 如10:00 * @return * @return * @throws IllegalArgumentException */ public static boolean isInTime(String sourceTime, String curTime) { if (sourceTime != null || sourceTime.contains("-") || sourceTime.contains(":")) { String[] args = sourceTime.split("-"); for(int i=0; i<args.length; i++){ if(!isValidDate(args[i])){ throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime); } } }else{ throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime); } if (curTime != null || curTime.contains(":")) { if(!isValidDate(curTime)){ throw new IllegalArgumentException("Illegal Argument arg:" + curTime); } } else{ throw new IllegalArgumentException("Illegal Argument arg:" + curTime); } String[] args = sourceTime.split("-"); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); try { long now = sdf.parse(curTime).getTime(); long start = sdf.parse(args[0]).getTime(); long end = sdf.parse(args[1]).getTime(); if (args[1].equals("00:00")) { args[1] = "24:00"; } if (end < start) { if (now >= end && now < start) { return false; } else { return true; } } else { if (now >= start && now < end) { return true; } else { return false; } } } catch (ParseException e) { e.printStackTrace(); throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime); } } }
相关文章推荐
- 判断当前时间是否在一个时间段范围内
- Oracle判断当前时间是否在范围呢
- 判断当前时间是否在某个时间范围内
- js判断输入日期时间是否在当前日期时间之前
- 判断手机号的输入是否正确、获取当前的时间以及显示距离上次发布有多久的时间
- 判断当前时间是否在某个时间范围内:比如:在8:00~20:00
- iOS 判断当前时间是否在一个时间范围内
- js判断输入时间是否大于当前时间
- 判断用户输入的银行卡号是否正确的方法(基于Luhn算法的格式校验)
- js 判断日期大小、是否在时间范围内等处理
- 判断邮箱,手机号码,身份证,用户名,昵称等输入格式是否正确的 正则表达式
- 判断是否为YYYY/MM/DD格式的时间
- iOS中利用NSDateComponents,NSDate,NSCalendar判断当前时间是否在一天的某个时刻段内
- Java判断当前时间是否在时间段中
- ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。
- android 用户注册或登录时判断输入的是否是正确格式的电话号码
- oracle 根据sysdate判断当前时间是否为正常工作时间
- Java用正则表达式判断输入的电话号码格式是否正确
- js判断日期/时间格式是否有效!
- java 小方法 判断字符串是否为时间格式,email格式,手机号格式