求一个算法,算女朋友的日子

ChiChengIT 2013-03-20 01:26:46
我女朋友上班是上2天休3天,2013年3月12日、13日上班,14、15、16不上班,17、18属于上班的日子以此类推,想写一段代码:

输入一个日期,(年月日格式)算出当天是否在休息日内,若在,列出这次休息的哪三天,若不在,把前后最近的3天休息日列出来,求高手指点
...全文
366 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅哥不解释 2013-03-21
  • 打赏
  • 举报
回复
顶LZ,有思想,有行动。
Tiger-3D 2013-03-21
  • 打赏
  • 举报
回复
次奥,这样太简单了吧
zhengxiu001 2013-03-21
  • 打赏
  • 举报
回复
学习。。。。。。。。。。。。
黑石课堂 2013-03-21
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
好像很厉害
ChiChengIT 2013-03-21
  • 打赏
  • 举报
回复
代码如下: ===================================================================== public class FreeDay { public static void main(String[] args) throws ParseException, IOException { String startStr = "2013-03-17"; String endStr = "2023-01-01"; Map freeMap = getFreeDate(startStr, endStr); List<String> allDayList= getAllDate(startStr,endStr); Scanner sc = new Scanner(System.in); System.out.println("\n\t\t\t===================欢迎使用休假查询系统(试用版1.0)========================"); System.out.println("\n\t\t\t ---------------- 使用说明: ★为休假日(★★为周六、周日重叠休假日),☆为上班日------------\n"); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------"); String a =""; Boolean flag = true; InputStreamReader is_reader = new InputStreamReader(System.in); while(flag){ do{ System.out.println("请选择操作界面(输入相应界面序号即可)\n" + "\t1.通用查询(支持2013年往后十年内查询)\n" + "\t2.精确查询(支持2013年往后千年内查询)\n" + "\t3.退出"); System.out.print("请输入:"); a = new BufferedReader(is_reader).readLine(); }while(!checkNum(a)); int sw = Integer.parseInt(a); switch(sw){ //1.通用查询 case 1: commonQuery(freeMap,allDayList); break; // 2.精确查询 case 2: jingqueQuery(); break; //*** 在此可添加扩展*** //3.退出 case 3: SetOut(); break; default: System.out.println("友情提示:"+sw+"功能尚未开发,请选择正确的操作界面"); } System.out.println("\n请选择是否继续?(y/n):"); String isOrNot = sc.next(); if("y".equals(isOrNot)){ flag = true; }else{ flag = false; } } SetOut(); } /**1.通用查询 * @param freeMap * @throws ParseException */ private static void commonQuery(Map freeMap,List<String> allDayList) throws ParseException { //获取所有天数和星期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdfFree = new SimpleDateFormat("yyyy/MM/dd"); sdf.setLenient(false); sdfFree.setLenient(false); SimpleDateFormat formatD=new SimpleDateFormat("E"); int i = 1; for (String s:allDayList){ // 休假 if(freeMap.get(s)!=null){ if("星期六".equals(formatD.format(sdf.parse(s)))||"星期日".equals(formatD.format(sdf.parse(s)))){ System.out.print("★★"+sdfFree.format(sdf.parse(s))+"("+formatD.format(sdf.parse(s))+") "); }else{ System.out.print("★"+sdfFree.format(sdf.parse(s))+"("+formatD.format(sdf.parse(s))+") "); } i++; } else{ System.out.print("☆"+s+"("+formatD.format(sdf.parse(s))+") "); i++; } if(i%6==0){ System.out.println("\n"); } } } /** * 2.精确查询(支持2013年1月1日年-3013年1月1人的查询) * @param freeMap * @throws IOException * @throws ParseException */ private static void jingqueQuery() throws IOException, ParseException { String startStr = "2013-03-17"; String endStr = "3023-01-01"; Map freeMap =getFreeDate(startStr, endStr); //先定义接受用户输入的变量 String currentDay; do{ //输出提示文字 System.out.print("请输入您要查询的日期:"); InputStreamReader is_reader = new InputStreamReader(System.in); currentDay = new BufferedReader(is_reader).readLine(); }while(!checkDay(currentDay));//当用户输入无效的时候,反复提示要求用户输入 if(freeMap.get(currentDay)!=null){ System.out.println(currentDay+"为休假日"); }else{ System.out.println(currentDay+"为上班日:"); } } /** * 获取时间段内的所有天数、星期 * @param startDayStr * @param endDayStr * @return * @throws ParseException */ private static List<String> getAllDate(String startDayStr,String endDayStr) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat formatD=new SimpleDateFormat("E");//"E"表示"day in week" Date startDay = sdf.parse(startDayStr); Date endDay = sdf.parse(endDayStr); List allDaylist =new ArrayList(); Long existDays =(endDay.getTime() -startDay.getTime())/(1000*24*3600); for(int i = 1;i<=existDays;i++){ Calendar calendar=Calendar.getInstance(); calendar.setTime(startDay); calendar.add(Calendar.DATE, i-1); // 加一天 Date date=calendar.getTime(); //获得加后的一天日期 allDaylist.add(sdf.format(date)); } return allDaylist; } /** * 获取所有休息的天数 * @param startDayStr * @param endDayStr * @return * @throws ParseException */ private static Map getFreeDate(String startDayStr,String endDayStr) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date startDay = sdf.parse(startDayStr); Date endDay = sdf.parse(endDayStr); Long existDays =(endDay.getTime() -startDay.getTime())/(1000*24*3600); Map map =new HashMap(); List freeDaylist = new ArrayList(); List tempList = new ArrayList(); for(int i = 1;i<=existDays;i++){ Calendar calendar=Calendar.getInstance(); calendar.setTime(startDay); calendar.add(Calendar.DATE, i-1); // 加一天 Date date=calendar.getTime(); //获得加后的一天日期 tempList.add(sdf.format(date)); if(tempList.size()>=5||i==existDays){ List list =new ArrayList(); if(tempList.size()>=2){ list =tempList.subList(2, tempList.size()); } freeDaylist.addAll(list); tempList.clear(); } } for (int i = 0; i < freeDaylist.size(); i++) { String date = (String)freeDaylist.get(i); map.put(date, "true"); } return map; } /** * 判断用户的合法日期校验 * @param money * @param cMoney * @return */ public static boolean checkDay(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setLenient(false); try{ sdf.parse(date); return true; } catch (Exception e) { System.out.println("友情提示:您输入的日期:"+date+",格式不对!请重新输入:"); return false; } } /** * 校验是否为数字 * @param num * @return */ public static boolean checkNum(String num) { try{ Integer s = new Integer(num) ; return true; } catch (Exception e) { System.out.println("您输入的号码:"+num+"不是数字!请重新选择"); return false; } } //退出 public static void SetOut() { System.out.println("===============谢谢使用,若需改善请联系管理员->魏帅==============="); System.exit(0); }
ChiChengIT 2013-03-21
  • 打赏
  • 举报
回复
昨天已经搞定
madStone_l 2013-03-21
  • 打赏
  • 举报
回复
低调飘过~!
x1n 2013-03-21
  • 打赏
  • 举报
回复
求图。
Iam太陽神 2013-03-21
  • 打赏
  • 举报
回复
引用 24 楼 zwf0731 的回复:
算完之后呢.........
想干吗就干嘛!
  • 打赏
  • 举报
回复
算完之后呢.........
纯唇Yu弄 2013-03-21
  • 打赏
  • 举报
回复
我以为算什么日子呢
woshi_ziyu 2013-03-21
  • 打赏
  • 举报
回复
黑石课堂 2013-03-21
  • 打赏
  • 举报
回复
delete:删除
月宅 2013-03-21
  • 打赏
  • 举报
回复
为嘛 水区还有要程序的 而且竟然还有回复代码的
文俊2014 2013-03-21
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
232013312131415161718 3
shen_wei 2013-03-20
  • 打赏
  • 举报
回复
这个应该不难。。你算出日历一次类推而已!!
shen_wei 2013-03-20
  • 打赏
  • 举报
回复
这么牛逼的工作。。。白富美啊
  • 打赏
  • 举报
回复
引用 7 楼 ChiChengIT 的回复:
引用 4 楼 jiangxiaoliang18 的回复:你女朋友工作是干啥的?真舒服啊 高铁乘务员,
白富美啊
加载更多回复(7)

682

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧